JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

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


Description:

Below example shows how to sort user defined objects by using comparator object. You can include you own custom sorting logic with compare method. By passing comparator object to the TreeMap, you can sort the keys based on the logic provided inside the compare method.


Code:
package com.java2novice.treemap;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;

public class MyTMCompUserDefine {

	public static void main(String a[]){
		//By using name comparator (String comparison)
		TreeMap<Empl,String> tm = new TreeMap<Empl, String>(new MyNameComp());
		tm.put(new Empl("Ram",3000), "RAM");
		tm.put(new Empl("John",6000), "JOHN");
		tm.put(new Empl("Crish",2000), "CRISH");
		tm.put(new Empl("Tom",2400), "TOM");
		Set<Empl> keys = tm.keySet();
		for(Empl key:keys){
			System.out.println(key+" ==> "+tm.get(key));
		}
		System.out.println("===================================");
		//By using salary comparator (int comparison)
		TreeMap<Empl,String> trmap = new TreeMap<Empl, String>(new MySalaryComp());
		trmap.put(new Empl("Ram",3000), "RAM");
		trmap.put(new Empl("John",6000), "JOHN");
		trmap.put(new Empl("Crish",2000), "CRISH");
		trmap.put(new Empl("Tom",2400), "TOM");
		Set<Empl> ks = trmap.keySet();
		for(Empl key:ks){
			System.out.println(key+" ==> "+trmap.get(key));
		}
	}
}

class MyNameComp implements Comparator<Empl>{

	@Override
	public int compare(Empl e1, Empl e2) {
		return e1.getName().compareTo(e2.getName());
	}
}

class MySalaryComp implements Comparator<Empl>{

	@Override
	public int compare(Empl e1, Empl e2) {
		if(e1.getSalary() > e2.getSalary()){
			return 1;
		} else {
			return -1;
		}
	}
}

class Empl{
	
	private String name;
	private int salary;
	
	public Empl(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:
Name: Crish-- Salary: 2000 ==> CRISH
Name: John-- Salary: 6000 ==> JOHN
Name: Ram-- Salary: 3000 ==> RAM
Name: Tom-- Salary: 2400 ==> TOM
===================================
Name: Crish-- Salary: 2000 ==> CRISH
Name: Tom-- Salary: 2400 ==> TOM
Name: Ram-- Salary: 3000 ==> RAM
Name: John-- Salary: 6000 ==> JOHN
<< Previous Program | Next Program >>

List Of All TreeMap Sample Programs:

  1. Basic TreeMap Operations.
  2. How to iterate through TreeMap?
  3. How to copy Map content to another TreeMap?
  4. How to search a key in TreeMap?
  5. How to search a value in TreeMap?
  6. How to get all keys from TreeMap?
  7. How to get entry set from TreeMap?
  8. How to delete all elements from TreeMap?
  9. How to sort keys in TreeMap by using Comparator?
  10. How to sort keys in TreeMap by using Comparator with user define objects?
  11. How to get sorted sub-map from TreeMap?
  12. How to get first key element from TreeMap (Sorted Map)?
  13. How to get last key element from TreeMap (Sorted Map)?
  14. How to reverse sorted keys in a TreeMap?
Knowledge Centre
Prevent a method from being overridden
By specifying final keyword to the method you can avoid overriding in a subcalss. Similarlly one can use final at class level to prevent creating subclasses.
Famous Quotations
Be yourself; everyone else is already taken.
-- Oscar Wilde

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.