1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| class Info { final String key; final String value;
Info(String key, String value) { this.key = key; this.value = value; }
public String getKey() { return key; }
public String getValue() { return value; }
@Override public String toString() { return "Info{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}'; } }
List<Info> infos = new ArrayList<>(); infos.add(new Info("A", "B")); infos.add(new Info("C", "D")); Map<String, String> mp = infos.stream().collect(Collectors.toMap(Info::getKey, Info::getValue)); System.out.println(mp);
Map<Info, Map<String, String>> data = new HashMap<>(); data.put(new Info("A", "A"), new HashMap<>()); data.put(new Info("A", "B"), new HashMap<>()); data.put(new Info("A", "C"), new HashMap<>()); data.put(new Info("B", "A"), new HashMap<>()); data.put(new Info("B", "B"), new HashMap<>()); data.put(new Info("B", "C"), new HashMap<>()); Map<String, Map<Info, Map<String, String>>> rd = data.entrySet() .stream() .collect(Collectors.groupingBy(kv -> kv.getKey().getKey(), Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); System.out.println(rd);
{A=B, C=D} {A={Info{key='A', value='B'}={}, Info{key='A', value='A'}={}, Info{key='A', value='C'}={}}, B={Info{key='B', value='A'}={}, Info{key='B', value='B'}={}, Info{key='B', value='C'}={}}}
|