在 Java 8 中递归展平嵌套映射的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37462264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively Flatten values of nested maps in Java 8
提问by Ian2thedv
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?
给定 a Map<String, Object>
,其中值是 aString
或 another Map<String, Object>
,如何使用 Java 8 将映射展平为单个值列表?
Example:
例子:
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
I know it can be done like this:
我知道可以这样做:
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);
}
}
}
How can I do this with a Stream
?
我怎样才能做到这一点Stream
?
Edit:
编辑:
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)
);
}
回答by Modus Tollens
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.
您可以定义一个递归方法,该方法将一张地图展平并将其用作函数Stream#flatMap
或通过直接调用它来使用它。
Example:
例子:
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]
[值1,值2,值3.1,值3.2,值3.3.1,值3.3.2]