This question has already been answered:

let's say I have a map consisting of words and the number of repetitions in the text

TreeMap<String,Integer> treeMap = new TreeMap<>(); treeMap.put("CCC", 10); treeMap.put("KKK", 11); treeMap.put("AAA", 2); treeMap.put("BBB", 2); treeMap.put("DDD", 2); for(Map.Entry e : treeMap.entrySet()){ System.out.println(e.getKey()+" "+ e.getValue()); } 

output:

 AAA 2 BBB 2 CCC 10 DDD 2 KKK 11 

It is necessary that first sorted by the number of repetitions, if the words have the same number of repetitions, then sorted in lexicographical order.

must be like:

 KKK 11 CCC 10 AAA 2 BBB 2 DDD 2 

Reported as a duplicate by PashaPash members , Bald , Axifive , Saidolim , torokhkun on Dec 3 '15 at 5:34 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • here first answered, then checked for doubles. But then I have a comparator from the eighth Java :) - zRrr

2 answers 2

What you want cannot be done only with the help of TreeMap , since TreeMap sets the order only by key.

You need to count the number of repetitions, and then unload all pairs in SortedSet specifying a comparator (or in the List and call sort );

 public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put( "AAA", 2 ); map.put( "BBB", 2 ); map.put( "CCC", 10 ); map.put( "DDD", 2 ); map.put( "KKK", 11 ); System.out.println( map ); SortedSet<Map.Entry<String, Integer>> sorted = new TreeSet<Map.Entry<String,Integer>>( // сортируем сперва по значениям (в natural order, т.е. по возрастанию) Comparator.<Map.Entry<String, Integer>, Integer>comparing( Map.Entry::getValue ) .reversed() // по убыванию .thenComparing( Map.Entry::getKey ) // и по ключам по возрастанию ); sorted.addAll( map.entrySet() ); System.out.println( sorted ); } 
  • ATP, yes, probably so - ketchyn Nov.
  • Comparator.<Map.Entry<String, Integer>, Integer>comparing( Map.Entry::getValue ) , I can’t talk about this syntoxis why there are such strange generics here, plz can be more detailed. - ketchyn
  • Comparator.comparing accepts a java.util.Function object, the apply method of which removes from the argument (in this case Map.Entry ) a field of type Comparable by which the comparator will compare. Why Eclipse could not display all types, I can not say yet, I will try to look later. - zRrr

If you want to have a Map as a result, the best option is to sort the records and LinkedHashMap them into LinkedHashMap in the correct order. TreeMap should not be sorted using values, it can lead to psychedelic effects. You can, for example, do this (Java-8):

 Map<String, Integer> result = map.entrySet().stream() .sorted(Entry.<String, Integer> comparingByValue().reversed() .thenComparing(Entry.comparingByKey())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> { throw new IllegalStateException(); }, LinkedHashMap::new)); 

I have the same comparator as in the @zRrr solution, only shorter.