I have a map like this:
Map<String,Map<String,String>>
And I need to sort the second map, so I try something like this:
ArrayList<ArrayList<String>> response = new ArrayList<>();
response.add(new ArrayList<String>() {{ add("STATE1"); add("COB2"); add("1"); }});
response.add(new ArrayList<String>() {{ add("STATE1"); add("COB125"); add("3");}});
response.add(new ArrayList<String>() {{ add("STATE1"); add("COB55"); add("4");}});
response.add(new ArrayList<String>() {{ add("STATE2"); add("UI5"); add("100");}});
Supplier<TreeMap<String, String>> typeMap = () -> new TreeMap<>(Comparator.naturalOrder());
Object result = response.stream().collect(groupingBy((ArrayList p) -> p.get(0).toString(), Collectors.toMap(
(ArrayList p) -> p.get(1).toString(),
(ArrayList p) -> p.get(2).toString(), (u, v) -> v,typeMap)));
Map<String, Map<String, String>> finalMap = (Map<String, Map<String, String>>) result;
finalMap = STATE1 => {COB125=3, COB2=1, COB55=4}, STATE2 => {UI5=100}
I need different sort type, I tried with the Comparator.naturalOrder()
But I need:
expectedMap STATE1 => {COB2=3, COB55=1, COB125=4}, STATE2 => {UI5=100}
response? What is anActionsList? What ismainList? Please take the tour, visit the help center and read How to Ask to learn how to use this site effectively. Consider reducing the code to a minimal reproducible example that others can actually execute... you'll probably figure out the problem in the process of doing that (a very useful exercise when confronting a difficult problem).