Adding user define class in HashSet in java

The example which adds user define class in HashSet.

package addinguserdefineobjectinhaset;

import java.util.*;
class Employee{
	String name;
	int id;
	Employee(int i,String s){
		this.id=i;
		this.name=s;
	}
 
	
}
public class Example {
	public static void main(String[] args) {
		Set<Employee> empset=new HashSet<>();
		Employee e1=new Employee(101,"sita");
		Employee e2=new Employee(101,"sita");
		Employee e3=new Employee(101,"ram");
		Employee e4=new Employee(101,"ram");
		
		empset.add(e1);
		empset.add(e2);
		empset.add(e3);
		empset.add(e4);
		
		for(Employee e:empset) {
			System.out.println(e.id+"  "+e.name);
		}
	}
 
}


 

Output is –

101 ram
101 sita
101 sita
101 ram