Dato un Map<String, Object>
, dove i valori sono o String
o un altro Map<String, Object>
, in che modo uno, utilizzando Java 8, appiattisce le mappe in un unico elenco di valori?Appiattisci in modo ricorsivo i valori delle mappe nidificate in Java 8
Esempio:
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"
Per l'esempio di cui sopra, vorrei il seguente elenco:
value1
value2
value3.1
value3.2
value3.3.1
value3.3.2
so che può essere fatto in questo modo:
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);
}
}
}
Come può Lo faccio con uno Stream
?
Edit:
Dopo un po 'di gioco intorno ho capito:
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)
);
}
http://stackoverflow.com/questions/21646683/recursive-stream suggerisce di utilizzare un riferimento al metodo. – jaco0646