从地图列表中提取所有字符串值
Extract all String values from a list of map
我收到了以下格式的休息回复。
{
"query_result": {
"status": "SUCCESS",
"message": "Results Found",
"results": [
{
"key": "123"
},
{
"key": "465"
}
]
}
}
从这个响应中,我想从键创建一个字符串列表。
因此希望获得如下列表结果:
["123", "456"]
正在尝试生成如下输出,但卡在了后续代码块中的映射步骤。
public Optional<List<String>> get(HttpEntity<Request> request){
ResponseEntity<Response> response = getResponse(request); // this response holds above json data
return Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(results -> results.) // Stuck here. results here is the list of results shown in above json.
}
我无法从这里提取所有密钥的值。
我最终不得不如下所示指示索引,因为我想要所有键,所以这是行不通的。
.map(results -> results.get(0))
尝试考虑使用迭代器,但那样只会再次捕获第一个键。
.filter(CollectionUtils::isNotEmpty)
.map(List::iterator)
.map(Iterator::next)
.map(result -> result.getKey())
我能得到一些帮助吗?谢谢。
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(results -> results.stream().map(Key::getKey).collect(Collectors.toList()));
假设每个键对象都正确映射到名为 Key
的 class,如下所示:
class Key {
String key;
String getKey() {
return key;
}
}
您希望在您的 Optional
链上将其内部 Key
列表转换为 String
列表。所以最后一步只是处理那部分。
备注
您的方法return是一个可选列表。一般来说,如果你 return 一个空列表而不是 null
在语义上更好,但这取决于实现。
如果理解 lambda 变得越来越困难,请将它们的逻辑转移到方法中。例如上面的链可以变成:
List<String> collectKeys(List<Key> keys) {
return keys.stream().map(Key::getKey).collect(Collectors.toList())
}
// and then:
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(this::collectKeys);
我收到了以下格式的休息回复。
{
"query_result": {
"status": "SUCCESS",
"message": "Results Found",
"results": [
{
"key": "123"
},
{
"key": "465"
}
]
}
}
从这个响应中,我想从键创建一个字符串列表。 因此希望获得如下列表结果:
["123", "456"]
正在尝试生成如下输出,但卡在了后续代码块中的映射步骤。
public Optional<List<String>> get(HttpEntity<Request> request){
ResponseEntity<Response> response = getResponse(request); // this response holds above json data
return Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(results -> results.) // Stuck here. results here is the list of results shown in above json.
}
我无法从这里提取所有密钥的值。 我最终不得不如下所示指示索引,因为我想要所有键,所以这是行不通的。
.map(results -> results.get(0))
尝试考虑使用迭代器,但那样只会再次捕获第一个键。
.filter(CollectionUtils::isNotEmpty)
.map(List::iterator)
.map(Iterator::next)
.map(result -> result.getKey())
我能得到一些帮助吗?谢谢。
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(results -> results.stream().map(Key::getKey).collect(Collectors.toList()));
假设每个键对象都正确映射到名为 Key
的 class,如下所示:
class Key {
String key;
String getKey() {
return key;
}
}
您希望在您的 Optional
链上将其内部 Key
列表转换为 String
列表。所以最后一步只是处理那部分。
备注
您的方法return是一个可选列表。一般来说,如果你 return 一个空列表而不是
null
在语义上更好,但这取决于实现。如果理解 lambda 变得越来越困难,请将它们的逻辑转移到方法中。例如上面的链可以变成:
List<String> collectKeys(List<Key> keys) {
return keys.stream().map(Key::getKey).collect(Collectors.toList())
}
// and then:
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(this::collectKeys);