简体   繁体   中英

How can I sort a ConcurrentHashMap by values?

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?

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));
}
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());
    }
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()));

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());
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM