Friday, May 10, 2019

How to sort keys in TreeMap by using Comparator with user define objects?

Problem statement:
How to sort keys in TreeMap by using Comparator with user defined objects.
  1. import java.util.Comparator;
  2. import java.util.Set;
  3. import java.util.TreeMap;
  4. public class SortByKeyUserDefine {
  5.     public static void main(String a[]) {
  6.         //By using name comparator (String comparison)
  7.         System.out.println("========= Sort by name ==============");
  8.         TreeMap<Employee, String> tm = new TreeMap<Employee, String>(new MyNameComp());
  9.         tm.put(new Employee("Ram", 3000), "RAM");
  10.         tm.put(new Employee("John", 6000), "JOHN");
  11.         tm.put(new Employee("Crish", 2000), "CRISH");
  12.         tm.put(new Employee("Tom", 2400), "TOM");
  13.         Set<Employee> keys = tm.keySet();
  14.         for (Employee key : keys) {
  15.             System.out.println(key + " ==> " + tm.get(key));
  16.         }
  17.         System.out.println("========= Sort by salary ==============");
  18.         //By using salary comparator (int comparison)
  19.         TreeMap<Employee, String> trmap = new TreeMap<Employee, String>(new MySalaryComp());
  20.         trmap.put(new Employee("Ram", 3000), "RAM");
  21.         trmap.put(new Employee("John", 6000), "JOHN");
  22.         trmap.put(new Employee("Crish", 2000), "CRISH");
  23.         trmap.put(new Employee("Tom", 2400), "TOM");
  24.         Set<Employee> ks = trmap.keySet();
  25.         for (Employee key : ks) {
  26.             System.out.println(key + " ==> " + trmap.get(key));
  27.         }
  28.     }
  29. }

  30. class MyNameComp implements Comparator<Employee> {
  31.     @Override
  32.     public int compare(Employee e1, Employee e2) {
  33.         return e1.getName().compareTo(e2.getName());
  34.     }
  35. }

  36. class MySalaryComp implements Comparator<Employee> {
  37.     @Override
  38.     public int compare(Employee e1, Employee e2) {
  39.         if (e1.getSalary() > e2.getSalary()) {
  40.             return 1;
  41.         } else {
  42.             return -1;
  43.         }
  44.     }
  45. }

  46. class Employee {
  47.     private String name;
  48.     private int salary;
  49.     public Employee(String n, int s) {
  50.         this.name = n;
  51.         this.salary = s;
  52.     }
  53.     public String getName() {
  54.         return name;
  55.     }
  56.     public void setName(String name) {
  57.         this.name = name;
  58.     }
  59.     public int getSalary() {
  60.         return salary;
  61.     }
  62.     public void setSalary(int salary) {
  63.         this.salary = salary;
  64.     }
  65.     public String toString() {
  66.         return "Name: " + this.name + " , Salary: " + this.salary;
  67.     }
  68. }
Output:
========= Sort by name ==============
Name: Crish , Salary: 2000 ==> CRISH
Name: John , Salary: 6000 ==> JOHN
Name: Ram , Salary: 3000 ==> RAM
Name: Tom , Salary: 2400 ==> TOM
========= Sort by salary ==============
Name: Crish , Salary: 2000 ==> null
Name: Tom , Salary: 2400 ==> null
Name: Ram , Salary: 3000 ==> null
Name: John , Salary: 6000 ==> null

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...