Checkout for Promo Codes

Program: How to avoid duplicate user defined objects in TreeSet?


To avoid duplicate user defined objects in TreeSet, you have to implement Comparator interface with equality verification. Below example gives a sample code to implement it.

package com.java2novice.treeset;

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

public class MyUserDuplicates {

	public static void main(String a[]){
		Set<Emp> ts = new TreeSet<Emp>(new EmpComp());
		ts.add(new Emp(201,"John",40000));
		ts.add(new Emp(302,"Krish",44500));
		ts.add(new Emp(146,"Tom",20000));
		ts.add(new Emp(543,"Abdul",10000));
		ts.add(new Emp(12,"Dinesh",50000));
		//adding duplicate entry
		ts.add(new Emp(146,"Tom",20000));
		//check duplicate entry is there or not
		for(Emp e:ts){

class EmpComp implements Comparator<Emp>{

	public int compare(Emp e1, Emp e2) {
		if(e1.getEmpId() == e2.getEmpId()){
			return 0;
		} if(e1.getEmpId() < e2.getEmpId()){
			return 1;
		} else {
			return -1;

class Emp {
	private int empId;
	private String empName;
	private int empSal;
	public Emp(int id, String name, int sal){
		this.empId = id;
		this.empName = name;
		this.empSal = sal;
	public int getEmpId() {
		return empId;
	public void setEmpId(int empId) {
		this.empId = empId;
	public String getEmpName() {
		return empName;
	public void setEmpName(String empName) {
		this.empName = empName;
	public int getEmpSal() {
		return empSal;
	public void setEmpSal(int empSal) {
		this.empSal = empSal;
	public String toString(){
		return empId+" : "+empName+" : "+empSal;

543 : Abdul : 10000
302 : Krish : 44500
201 : John : 40000
146 : Tom : 20000
12 : Dinesh : 50000
<< Previous 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
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

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 © 2022 by Nataraja Gootooru. All Rights Reserved.