我正在以List<Map<String, Object>>
格式从数据库查询中检索结果,您可以建议如何将其转换为List<Map<String, String>>
。
迭代列表,依次变换每个地图:
list.stream()
.map(map ->
map.entrySet().stream()
.collect(
Collectors.toMap(
Entry::getKey, e -> e.getValue().toString())))
.collect(Collectors.toList())
我想出了这种方法。缺点是新地图是不可变的。
list.stream()
.map(map -> map.entrySet()
.stream()
.collect(Collectors.toMap(Entry::getKey,
e -> e.getValue().toString())))
.collect(Collectors.toList());
为了缓解该问题,Map.of
构造可以作为HashMap
构造函数的参数。