How can I sort a ConcurrentHashMap by values?

后端 未结 3 871
南方客
南方客 2021-01-16 14:59
ConcurrentHashMap pl = new ConcurrentHashMap<>();
pl.put(\"joker25\", 255);
pl.put(\"minas\", 55);
pl.put(\"agoriraso\", 122);
pl.put(\"p         


        
相关标签:
3条回答
  • 2021-01-16 15:35
    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()));
    
    0 讨论(0)
  • 2021-01-16 15:42

    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));
    }
    
    0 讨论(0)
  • 2021-01-16 15:55
    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());
        }
    
    0 讨论(0)
提交回复
热议问题