[英]How to Convert List<Map<String, Object>> to List<Map<String, String>>
I am retrieving results from DB Query as List<Map<String, Object>>
format, can you suggest, How to convert it to List<Map<String, String>>
.我正在以List<Map<String, Object>>
格式从数据库查询中检索结果,您能否建议如何将其转换为List<Map<String, String>>
。
Iterate the list, transforming each of the maps in turn:迭代列表,依次转换每个映射:
list.stream()
.map(map ->
map.entrySet().stream()
.collect(
Collectors.toMap(
Entry::getKey, e -> e.getValue().toString())))
.collect(Collectors.toList())
A simple for-each iteration over the list items and its map entries does the trick:对列表项及其 map 条目进行简单的 for-each 迭代即可达到目的:
List<Map<String, Object>> list = ...
List<Map<String, String>> newList = new ArrayList<>();
for (Map<String, Object> map: list) {
Map<String, String> newMap = new HashMap<>();
for (Entry<String, Object> entry: map.entrySet()) {
newMap.put(entry.getKey(), entry.getValue().toString()); // mapping happens here
}
newList.add(newMap);
}
In my opinion, this is the optimal solution and the most readable solution.在我看来,这是最佳解决方案,也是最易读的解决方案。 The java-stream is not suitable much for working with dictionaries (although you always get a collection from it). java-stream不太适合处理字典(尽管你总是从中得到一个集合)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.