1
ConcurrentHashMap<String,Integer> pl = new ConcurrentHashMap<>();
pl.put("joker25", 255);
pl.put("minas", 55);
pl.put("agoriraso", 122);
pl.put("pigasakias", 1024);
pl.put("geo5", 5092);

I've searched and I can't find anything. How do I sort my ConcurrentHashMap by values?

minas,25
agoriraso,122
joker25,255
pigasakias,1024
geo5,5092

How can I do this?

nanofarad
  • 40,330
  • 4
  • 86
  • 117
ponyriding
  • 15
  • 1
  • 1
  • 3
  • 1
    You can't with a `ConcurrentHashMap` nor any `Map` for that matter since you want to sort by values; you'd need to put all `Map.Entry` elements in a `List` and write a `Comparator` for these `Map.Entry` elements. – fge Apr 22 '14 at 17:17
  • refer http://stackoverflow.com/questions/10592002/sorting-a-concurrent-hash-map-by-value – Shreyos Adikari Apr 22 '14 at 17:18
  • you can't sort a ConcurrentHashMap at all. you would have to use an alternate data structure if you want to sort (by key or by value). – jtahlborn Apr 22 '14 at 17:18
  • Have you considered using a [`ConcurrentSkipListMap`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListMap.html)? – assylias Apr 22 '14 at 17:49

4 Answers4

2

Since ConcurrentHashMap makes no guarantees about ordering you'll have to dump the items into a list and then sort that. For example:

final Map<String, Integer> pl = ....
List<String> values = new ArrayList<>(pl.keySet());
Collections.sort(values, new Comparator<String>() {
  public int compare(String a, String b) {
    // no need to worry about nulls as we know a and b are both in pl
    return pl.get(a) - pl.get(b);
  }
});

for(String val : values) {
  System.out.println(val + "," + pl.get(val));
}
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1
You can use the list to store the EntrySet values and then sort by using list.sort() method with the help of Comparator.


    ConcurrentHashMap<String,Integer> pl = new ConcurrentHashMap<>();
    pl.put("joker25", 255);
    pl.put("minas", 55);
    pl.put("agoriraso", 122);
    pl.put("pigasakias", 1024);
    pl.put("geo5", 5092);

    //Add your map elements into a list which accepts EntrySet<String, Integer> as Input
    List<Entry<String, Integer>> list = new ArrayList<>(pl.entrySet());

   /*
    * Sort the list by using comparator object or comparator lambda expression (as
    * listed in any one of the below ways)
    */

    // WAY - 1 : Passing Comparator as lambda Expression
    list.sort((e1, e2) -> e1.getValue().compareTo(e2.getValue()));

    /*
     * WAY - 2 : By using method reference for Comparator - Uncomment below line if
     * you want to use Method reference
     */
    //list.sort(Comparator.comparing(Entry::getValue));
    
   /*
     * Create a new HashMap and add the values to it, but here if you add the sorted
     * values to Concurrent hashMap you will not see the values in sorted order
     * because the entries will be stored according to hash value of string
     */

    //Suggest you to use the linked hashmap if you want to see the sorted order of entries according to the valueset
    ConcurrentHashMap<String,Integer> ol = new ConcurrentHashMap<>();

    //LinkedHashMap<String,Integer> ol = new LinkedHashMap<>();
    
    for(Entry<String, Integer> e : list) {
        System.out.println(e.getKey() +" ::" +e.getValue());
        ol.put(e.getKey(), e.getValue());
    }
Manjunath H M
  • 818
  • 1
  • 10
  • 25
0
LinkedList<Map.Entry<String, Integer>> list = new LinkedList<>(counter.entrySet());
Comparator<Map.Entry<String, Integer>> comparator = Comparator.comparing(Map.Entry::getValue);
Collections.sort(list, comparator.reversed());

and

Map<String, BigDecimal> reMap = new LinkedHashMap<>();
counter.entrySet().stream()
    .sorted(Map.Entry.<String, BigDecimal>comparingByValue().reversed())
    .forEachOrdered(eachOne -> reMap.put(eachOne.getKey(), eachOne.getValue()));
koon
  • 61
  • 2
0

We can easily iterate through a ConcurrentHashMap in the sorted order using java streams:

concurrentMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(entry -> {
            System.out.println(entry.getKey()+","+entry.getValue());
            }
Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55