java - Create a Map of SortedMap from a Map using lambda expression -
i have map having entries below
[{a.p=aa}, {a.r=aaaa}, {a.q=aaa}, {a.s=aaaaa}, {b.p=bb}, {b.r=bbbb}, {c.r=cccc}, {c.q=ccc}, {d.s=ddddd}, {d.p=ddd}]
i want 'map of sortedmap' below using lambda expression
[{a=[{a.p=aa}, {a.q=aaa}, {a.r=aaaa}, {a.s=aaaaa}]}, {b=[{b.p=bb}, {b.r=bbbb}]}, {c=[{c.q=ccc}, {c.r=cccc}]}, {d=[{d.p=aaaaa}, {d.s=ddddd}]}]
supplier<sortedmap<string, string>> supplier = new supplier<sortedmap<string, string>>() { @override public sortedmap<string, string> get() { return new treemap<string, string>((a, b) -> a.compareto(b)); } }; // [{a.p=aa}, {a.r=aaaa}, {a.q=aaa}, {a.s=aaaaa}, {b.p=bb}, {b.r=bbbb}, {c.r=cccc}, {c.q=ccc}, {d.s=ddddd}, {d.p=ddd}] map<string, string> map = new hashmap<string, string>(); map.put("a.p", "aa"); map.put("a.r", "aaaa"); map.put("a.q", "aaa"); map.put("a.s", "aaaaa"); map.put("b.p", "bb"); map.put("b.r", "bbbb"); map.put("c.r", "cccc"); map.put("c.q", "ccc"); map.put("d.s", "ddddd"); map.put("d.p", "aaaa"); map<string, sortedmap<string, string>> collect = map .entryset() .stream() .collect( collectors.groupingby((k -> k.getkey().substring(0, k.getkey().indexof('.'))), collectors.tomap(k -> ((entry<string, string>) k).getkey(), k -> ((entry<string, string>) k).getvalue(), (a, b) -> a, supplier)));
Comments
Post a Comment