fork download
  1. import java.util.HashMap;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Set;
  6. import java.util.TreeMap;
  7. import java.util.stream.Collectors;
  8.  
  9. class Example {
  10. public static void main(String[] args) {
  11. // prepare data..
  12. Map<String, Map<String, Integer>> mapOfMaps = new TreeMap<>();
  13. Map<String, Integer> m = new HashMap<>();
  14. m.put("x", 0);
  15. m.put("y", 43);
  16. m.put("z", 57);
  17. mapOfMaps.put("a", m);
  18. m = new HashMap<>();
  19. m.put("x", 1);
  20. m.put("y", 90);
  21. m.put("z", 9);
  22. mapOfMaps.put("b", m);
  23. m = new HashMap<>();
  24. m.put("x", 1);
  25. m.put("y", 83);
  26. m.put("z", 16);
  27. mapOfMaps.put("c", m);
  28.  
  29. // magic
  30. TreeMap<String, List<Integer>> series = mapOfMaps.values().stream()
  31. .map(Map::entrySet)
  32. .flatMap(Set::stream)
  33. .collect(Collectors.groupingBy(Entry::getKey, TreeMap::new,
  34. Collectors.mapping(Entry::getValue, Collectors.toList())));
  35.  
  36.  
  37. // print
  38. System.out.println("From: " + mapOfMaps);
  39. System.out.println("To: " + series);
  40. }
  41. }
Success #stdin #stdout 0.22s 321152KB
stdin
Standard input is empty
stdout
From: {a={x=0, y=43, z=57}, b={x=1, y=90, z=9}, c={x=1, y=83, z=16}}
To:   {x=[0, 1, 1], y=[43, 90, 83], z=[57, 9, 16]}