JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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


Description:

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.


Code:
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){
			System.out.println(e);
		}
	}
}

class EmpComp implements Comparator<Emp>{

	@Override
	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;
	}
}

Output:
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
Difference between Enumeration and Iterator
The functionality of Enumeration and the Iterator are same. You can get remove() from Iterator to remove an element, while while Enumeration does not have remove() method. Using Enumeration you can only traverse and fetch the objects, where as using Iterator we can also add and remove the objects. So Iterator can be useful if you want to manipulate the list and Enumeration is for read-only access.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

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.