Problem statement:
How to sort keys in TreeMap by using Comparator with user defined objects.
How to sort keys in TreeMap by using Comparator with user defined objects.
- import java.util.Comparator;
- import java.util.Set;
- import java.util.TreeMap;
- public class SortByKeyUserDefine {
- public static void main(String a[]) {
- //By using name comparator (String comparison)
- System.out.println("========= Sort by name ==============");
- TreeMap<Employee, String> tm = new TreeMap<Employee, String>(new MyNameComp());
- tm.put(new Employee("Ram", 3000), "RAM");
- tm.put(new Employee("John", 6000), "JOHN");
- tm.put(new Employee("Crish", 2000), "CRISH");
- tm.put(new Employee("Tom", 2400), "TOM");
- Set<Employee> keys = tm.keySet();
- for (Employee key : keys) {
- System.out.println(key + " ==> " + tm.get(key));
- }
- System.out.println("========= Sort by salary ==============");
- //By using salary comparator (int comparison)
- TreeMap<Employee, String> trmap = new TreeMap<Employee, String>(new MySalaryComp());
- trmap.put(new Employee("Ram", 3000), "RAM");
- trmap.put(new Employee("John", 6000), "JOHN");
- trmap.put(new Employee("Crish", 2000), "CRISH");
- trmap.put(new Employee("Tom", 2400), "TOM");
- Set<Employee> ks = trmap.keySet();
- for (Employee key : ks) {
- System.out.println(key + " ==> " + trmap.get(key));
- }
- }
- }
- class MyNameComp implements Comparator<Employee> {
- @Override
- public int compare(Employee e1, Employee e2) {
- return e1.getName().compareTo(e2.getName());
- }
- }
- class MySalaryComp implements Comparator<Employee> {
- @Override
- public int compare(Employee e1, Employee e2) {
- if (e1.getSalary() > e2.getSalary()) {
- return 1;
- } else {
- return -1;
- }
- }
- }
- class Employee {
- private String name;
- private int salary;
- public Employee(String n, int s) {
- this.name = n;
- this.salary = s;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getSalary() {
- return salary;
- }
- public void setSalary(int salary) {
- this.salary = salary;
- }
- public String toString() {
- return "Name: " + this.name + " , Salary: " + this.salary;
- }
- }
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