How to sort a hashmap by the Integer Value

37,268

Solution 1

Try this:

HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 4);
map.put("c", 6);
map.put("b", 2);
Object[] a = map.entrySet().toArray();
Arrays.sort(a, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Map.Entry<String, Integer>) o2).getValue()
                   .compareTo(((Map.Entry<String, Integer>) o1).getValue());
    }
});
for (Object e : a) {
    System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
            + ((Map.Entry<String, Integer>) e).getValue());
}

output:

c : 6
a : 4
b : 2

Solution 2

You can't explicity sort the HashMap, but can sort the entries. Maybe something like this helps:

// not yet sorted
List<Integer> intList = new ArrayList<Integer>(map.values());

Collections.sort(intList, new Comparator<Integer>() {

    public int compare(Integer o1, Integer o2) {
        // for descending order
        return o2 - o1;
    }
});
Share:
37,268
GameDevGuru
Author by

GameDevGuru

Updated on August 23, 2020

Comments

  • GameDevGuru
    GameDevGuru almost 4 years
    HashMap<String,Integer> map = new HashMap<String,Integer>();
    map.put("a", 4);
    map.put("c", 6);
    map.put("b", 2);
    

    Desired output(HashMap):

    c : 6
    a : 4
    b : 2
    

    I haven't been able to find anything about Descending the order by value.
    How can this be achieved? (Extra class not preferred)

  • Ashish
    Ashish over 10 years
    It would be a very great suggestion to keep in mind however it does not answer the question.
  • GameDevGuru
    GameDevGuru over 10 years
    Instead of saying it's impossible, how about suggesting to cast it as an ArrayList first..
  • user2864740
    user2864740 over 10 years
    I believe the OP wants to have the key and value together, in however the result is used.
  • GameDevGuru
    GameDevGuru over 10 years
    This separates the key from value, without the desired result..
  • GameDevGuru
    GameDevGuru over 10 years
    @user2864740 List<String, Integer> intList = new ArrayList<String,Integer>(map); , and then working with the list. Or something to that effect.
  • user2864740
    user2864740 over 10 years
    @GameDevGuru But that's not a cast at all! For all casts in Java, where x is a reference type conforming to T: ((T)x) == x is true.
  • Joseph Martin
    Joseph Martin over 10 years
    This is true, I figured he was printing them out or something, so all he'd have to do then is make a method that calls map.getKey(value) and returns a string...or whatever he needs.
  • GameDevGuru
    GameDevGuru over 10 years
    This puts the key,value pair as a single object in an array, I need to be able to retrieve the key, values seperately after sorting
  • GameDevGuru
    GameDevGuru over 10 years
    @user2864740 I have used the wrong terminology, i apologize for the confusion but I think you know what i meant.
  • Evgeniy Dorofeev
    Evgeniy Dorofeev over 10 years
    It puts Map.Entry you can extract key and value from it separately, see upate