Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting HashMap objects on their properties rather values [duplicate]

Tags:

java

hashmap

This is not my real code I have just simulated in order to understand what to do next.

I have class Person with properties age, height weight.
Now In my class Group
I create two four objects

Person programmer, student, clerk, tech;

I have HashMap rollCall

Map<Person, Integer> rollCall = new HashMap<Person, Integer>();

to add all these using Person and number of Persons as type Integer

rollCall.put(programmer, 1);
rollCall.put(clerk, 2);
rollCall.put(student, 1);
rollCall.put(tech, 3);

I have seen alot of people sorting HashMap using TreeMap on value I want to sort on a property of Person rather on value. I want to sort all these people on their age (i.e. programmer.getAge();). I am not sure if I will use comprator which works only on collection not map. . Please help ... .

like image 661
Aahil Avatar asked Jun 07 '11 01:06

Aahil


People also ask

How do you sort HashMap on the basis of values?

If we need to sort the HashMap by values, we should create a Comparator. It compares two elements based on the values. After that get the Set of elements from the Map and convert Set into the List. Use the Collections.

Can HashMap be sorted?

Java HashMap does not preserve any order by default. If there is a need to sort HashMap we sort it explicitly based on the requirements. Java provides an option to sort HashMap based on keys and values.

Does HashMap allow duplicate keys and values?

Duplicates: HashSet doesn't allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.

How do you sort HashMap values in descending order?

In order to sort in decreasing order, just reverse the order of Comparator using Collections. reverseOrder() or Comparator. reverse() method of Java 8.


2 Answers

You can get a Map<Person,Integer> which iterates by age increasing or decreasing order by using a custom comparator:

Map<Person, Integer> rollCall = new TreeMap<Person, Integer>(
  new Comparator<Person>() {
    @Override public int compare(Person p1, Person p2) {
      return p1.getAge() - p2.getAge(); // Acending.
      // or  p2.getAge() - p1.getAge(); // Descending.
    }
  }
);

When you add Persons to the collection they will be inserted in order by age.

like image 144
maerics Avatar answered Sep 30 '22 09:09

maerics


You need to be able to compare your Person objects. If there is a canonical way to compare them, let them implement Comparable<Person> (i.e. give them a compareTo(Person) method.

If this is done, you can use the persons as keys for a SortedMap (like TreeMap).

If there are multiple ways two persons could be compared, implement a Comparator<Person> as a separate object.

Then give this comparator to the SortedMap on construction.

This will not sort your HashMap (a HashMap has always a seemingly random order), but give you another sorted datastructure instead.

like image 23
Paŭlo Ebermann Avatar answered Sep 30 '22 11:09

Paŭlo Ebermann