JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: Create TreeSet with comparator by user define objects.


Description:

To implement your own sorting functionality with TreeSet on user defined objects, you have to pass Comparator object along with TreeSet constructor call. The Comparator implementation holds the sorting logic. You have to override compare() method to provide the sorting logic on user defined objects. Below example shows how to sort TreeSet using comparator with user defined objects.


Code:
package com.java2novice.treeset;

import java.util.Comparator;
import java.util.TreeSet;

public class MyCompUserDefine {

	public static void main(String a[]){
		//By using name comparator (String comparison)
		TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());
		nameComp.add(new Empl("Ram",3000));
		nameComp.add(new Empl("John",6000));
		nameComp.add(new Empl("Crish",2000));
		nameComp.add(new Empl("Tom",2400));
		for(Empl e:nameComp){
			System.out.println(e);
		}
		System.out.println("===========================");
		//By using salary comparator (int comparison)
		TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());
		salComp.add(new Empl("Ram",3000));
		salComp.add(new Empl("John",6000));
		salComp.add(new Empl("Crish",2000));
		salComp.add(new Empl("Tom",2400));
		for(Empl e:salComp){
			System.out.println(e);
		}
	}
}

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
Name: John-- Salary: 6000
Name: Ram-- Salary: 3000
Name: Tom-- Salary: 2400
===========================
Name: Crish-- Salary: 2000
Name: Tom-- Salary: 2400
Name: Ram-- Salary: 3000
Name: John-- Salary: 6000
<< Previous Program | Next Program >>

List Of All TreeSet Sample Programs:

  1. Basic TreeSet Operations.
  2. How to create a TreeSet with a List?
  3. How to read objects from TreeSet using using Iterator?
  4. Write a program to remove duplicate entries from an array.
  5. Write a program to find duplicate value from an array.
  6. How to create a TreeSet with comparator?
  7. Create TreeSet with comparator by user define objects.
  8. How to sort a TreeSet with user defined objects?
  9. How to get subset from sorted set?
  10. How to get least value element from a set?
  11. How to get highest value element from a set?
  12. How to avoid duplicate user defined objects in TreeSet?
Knowledge Centre
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

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.