Languages

Java - how to sort map by count of value of list eg Map<String, List<String>>

3 points
Asked by:
TheDalot
270

How to sort map by count of value of list eg Map<String, List<String>>?

What's the best way?

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Example {

    public static void main(String[] args) {

        Map<String, List<String>> map = new LinkedHashMap<>();
        map.put("A", Arrays.asList("1"));
        map.put("B", Arrays.asList("2", "3", "4", "5", "6"));
        map.put("C", Arrays.asList("7", "8"));
        map.put("D", Arrays.asList("9", "10", "11"));

        // sort by count and print

        // expected output:
        // key B - 5 values
        // key D - 3 values
        // key C - 2 values
        // key A - 1 values
    }
}

 

0 answers
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join