0

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}

2
  • 1
    What objects are in response? What is an ActionsList? What is mainList? 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). Commented Mar 9, 2022 at 23:49
  • @JimGarrison Thanks for the recommendation. question has been edited Commented Mar 10, 2022 at 0:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.