簡體   English   中英

如何按值對 ConcurrentHashMap 進行排序?

[英]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);

我已經搜索過,但找不到任何東西。 如何按值對 ConcurrentHashMap 進行排序?

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

我怎樣才能做到這一點?

由於ConcurrentHashMap不保證排序,因此您必須將項目轉儲到列表中,然后對其進行排序。 例如:

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

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

我們可以使用 Java 流輕松地按排序順序遍歷ConcurrentHashMap

concurrentMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(entry -> {
            System.out.println(entry.getKey()+","+entry.getValue());
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM