Sort HashMap objects by their properties

This is not my real code that I just modeled to figure out what to do next.

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

Person programmer, student, clerk, tech; 

I have a HashMap rollCall

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

to add all this using Person and the number of people as an Integer type

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

I have seen many people sorting a HashMap using TreeMap on value. I want to sort by Person property, not by value. I want to sort all of these people by their age (i.e. Programmer.getAge ();). I'm not sure if I will use a composite element that works only on the collection, not on the map. Please help ....

+4
source share
5 answers

You can get a Map<Person,Integer> , which iterates in ascending or descending order 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 Faces to the collection, they will be inserted by age.

+4
source

First of all, TreeMap sorts keys, not values. So I already work in your favor. Any object that you use as a key in TreeMap must implement Comparable , or you must provide Comparator as a constructor argument. All you have to do is use the compareTo() method (from Comparable ) or compare() (from Comparator ) based on your getAge() property.

The TreeMap constructor that hosts the Comparator is described here. Comparator will be used to sort keys into a map.

+1
source

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

If this is done, you can use people as keys for the SortedMap (e.g. TreeMap).

If there are several ways to compare two people, run Comparator<Person> as a separate object.

Then give this comparator to the SortedMap when building.

This will not sort your HashMap (HashMap always has a random order), but instead you will get another sorted data structure.

+1
source
 import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class PersonSort { private MySort sort = new MySort(); private Map<Person, String> map = new HashMap<Person, String> (); private Map<Person, String> treeMap = new TreeMap<Person, String>(sort); Person e1 = new Person(500, "Saurabh"); Person e2 = new Person(400, "Kishan"); Person e3 = new Person(900, "Ashwini"); public void myMap() { map.put(e3, "Ash"); map.put(e2, "Krish"); map.put(e1, "Sau"); Iterator it = map.keySet().iterator(); System.out.println("UnSorted Map"); while(it.hasNext()) { System.out.println(map.get(it.next())); } treeMap.putAll(map); System.out.println("SortedMap"); Iterator it1 = treeMap.keySet().iterator(); while(it1.hasNext()) { System.out.println(treeMap.get(it1.next())); } } public static void main(String[] args) { PersonSort es = new PersonSort(); es.myMap(); } } class Person { Person(int id, String name) { this.id = id; this.name = name; } private int id; private String name; //Getters and Setters } class MySort implements Comparator<Object> { public int compare(Object o1, Object o2) { return ((Person) o1).getId() - ((Person)o2).getId(); } } 
0
source
 import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; /* * Sort HashMap that contains Student object */ public class SortHashMap implements Comparator<Student> { public static void main(String[] args) { Map map = new HashMap(); map.put("s1", new Student(5,"utpal")); map.put("s2", new Student(4,"ramesh")); map.put("s3", new Student(10,"tushar")); map.put("s4", new Student(2,"anindya")); Collection<Student> students = map.values(); List list = new ArrayList(students); Collections.sort(list,new SortHashMap()); for (Iterator it = list.iterator(); it.hasNext();) { Student stdn = (Student)it.next(); System.out.println("Student id : "+stdn.id); System.out.println("Student Name : "+stdn.name); } } @Override public int compare(Student s1, Student s2) { return s1.name.compareTo(s2.name); } } class Student { int id; String name; Student(int id,String name) { this.id = id; this.name = name; } } 
-1
source

All Articles