47

I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8.

HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 10);
        map.put("b", 30);
        map.put("c", 50);
        map.put("d", 40);
        map.put("e", 100);
        map.put("f", 60);
        map.put("g", 110);
        map.put("h", 50);
        map.put("i", 90);
        map.put("k", 70);
        map.put("L", 80);

I know before Java 8, we can sort as this link: https://stackoverflow.com/a/109389/4315608

Lii
  • 11,553
  • 8
  • 64
  • 88
Sudarsana Kasireddy
  • 922
  • 1
  • 8
  • 24

4 Answers4

98

You can always start reading the documentation and some tutorials.

map.entrySet().stream()
        .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) 
        .limit(10) 
        .forEach(System.out::println); // or any other terminal method

Reference

http://www.leveluplunch.com/java/examples/sort-order-map-by-values/

ericbn
  • 10,163
  • 3
  • 47
  • 55
  • 6
    `.stream().sorted(`[`Map.Entry.comparingByValue()`](http://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html#comparingByValue--)`.reversed()) …` By the way, if you stick with your custom `Comparator` you may make it reversed in the first place: `(a,b) -> b.getValue().compareTo(a.getValue())` – Holger Apr 10 '15 at 18:13
  • does `.limit(10)` means a limit to first 10 results? – Arun Gowda Nov 03 '18 at 07:33
  • 1
    @Arun, correct. Question is about how to "take only top 10 entries". – ericbn Nov 03 '18 at 13:57
  • Thanks not only for pointing out `Map.Entry.comparingByValue()`, but also for showing the necessary explicit type necessary to get around [Java's weak type inference](https://stackoverflow.com/a/25173599) for these sort of cases. – Garret Wilson Aug 03 '23 at 22:12
18

See the stack flow thread here and example here

Map<String,Person> map = new HashMap<>();
map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));

Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
        e1.getValue().getLocation().compareTo(e2.getValue().getLocation()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                (e1, e2) -> e1, LinkedHashMap::new));
sortedNewMap.forEach((key,val)->{
    System.out.println(key+ " = "+ val.toString());
});
Community
  • 1
  • 1
Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44
4
List<Map.Entry<String, Integer>> entries = newArrayList(map.entrySet());
Collections.sort(entries, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
List<Map.Entry<String, Integer>> top10 = entries.subList(0, Math.min(entries.size(), 10));
pomo
  • 2,251
  • 1
  • 21
  • 34
4

If you want sort by integer or float value of value object

Map<String,Person> map = new HashMap<>();
map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));

You can use,

Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
        Integer.compare(e1.getValue().getAge(), e2.getValue().getAge()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                (e1, e2) -> e1, LinkedHashMap::new));
Nycrera
  • 49
  • 2
  • 1
    It is essentially the same answer as [this one](https://stackoverflow.com/a/37224797/7470253). Why did you feel you need to do that? – M. Prokhorov Mar 22 '18 at 12:17
  • Yes you are correct, but maybe someone don't know comparing by int (Integer.compare()) or float (Float.compare()) of object. – Nycrera Mar 22 '18 at 12:25
  • Using specialized comparator methods doesn't have anything to do with answering the OP's question though. – M. Prokhorov Mar 22 '18 at 12:47