Recursively Flatten values of nested maps in Java 8(Java 8 中嵌套映射的递归展平值)
问题描述
给定一个 Map
,其中的值是 String
或另一个 Map
,如何有人会使用 Java 8 将地图展平为单个值列表吗?
Given a Map<String, Object>
, where the values are either a String
or another Map<String, Object>
, how would one, using Java 8, flatten the maps to a single list of values?
例子:
Map - "key1" -> "value1"
- "key2" -> "value2"
- "key3" -> Map - "key3.1" -> "value3.1"
- "key3.2" -> "value3.2"
- "key3.3" -> Map - "key3.3.1" -> "value3.3.1"
- "key3.3.2" -> "value3.3.2"
对于上面的例子,我想要以下列表:
For the above example, I would like the following list:
value1
value2
value3.1
value3.2
value3.3.1
value3.3.2
我知道可以这样做:
public static void main(String args[]) throws Exception {
//Map with nested maps with nested maps with nested maps with nested......
Map<String, Object> map = getSomeMapWithNestedMaps();
List<Object> values = new ArrayList<>();
addToList(map, values);
for (Object o:values) {
System.out.println(o);
}
}
static void addToList(Map<String, Object>map, List<Object> list) {
for (Object o:map.values()) {
if (o instanceof Map) {
addToList((Map<String, Object>)o, list);
} else {
list.add(o);
}
}
}
如何使用 Stream
做到这一点?
How can I do this with a Stream
?
玩了一会儿,我想通了:
After some playing around I figured it out:
public static void main(String args[]) throws Exception {
//Map with nested maps with nested maps with nested maps with nested......
Map<String, Object> map = getSomeMapWithNestedMaps();
//Recursively flatten maps and print out all values
List<Object> list= flatten(map.values().stream()).collect(Collectors.toList());
}
static Stream<Object> flatten(Stream<Object> stream) {
return stream.flatMap((o) ->
(o instanceof Map) ? flatten(((Map<String, Object>)o).values().stream()) : Stream.of(o)
);
}
推荐答案
你可以定义一个递归方法,将一个地图展平,并将其用作 Stream#flatMap
的函数,或者通过调用它来使用它直接.
You could define a recursive method which flattens one map and use it as a function for Stream#flatMap
or use it by calling it directly.
例子:
public class FlatMap {
public static Stream<Object> flatten(Object o) {
if (o instanceof Map<?, ?>) {
return ((Map<?, ?>) o).values().stream().flatMap(FlatMap::flatten);
}
return Stream.of(o);
}
public static void main(String[] args) {
Map<String, Object> map0 = new TreeMap<>();
map0.put("key1", "value1");
map0.put("key2", "value2");
Map<String, Object> map1 = new TreeMap<>();
map0.put("key3", map1);
map1.put("key3.1", "value3.1");
map1.put("key3.2", "value3.2");
Map<String, Object> map2 = new TreeMap<>();
map1.put("key3.3", map2);
map2.put("key3.3.1", "value3.3.1");
map2.put("key3.3.2", "value3.3.2");
List<Object> collect = map0.values().stream()
.flatMap(FlatMap::flatten)
.collect(Collectors.toList());
// or
List<Object> collect2 = flatten(map0).collect(Collectors.toList());
System.out.println(collect);
}
}
对于给定的嵌套地图,它会打印
For the given nested map, it prints
[value1, value2, value3.1, value3.2, value3.3.1, value3.3.2]
[value1, value2, value3.1, value3.2, value3.3.1, value3.3.2]
这篇关于Java 8 中嵌套映射的递归展平值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 8 中嵌套映射的递归展平值
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01