Clean Java

There is always a bigger game

Unique collection in java.

leave a comment »

Set implementation in java does not allow duplicate elements in it. Java uses the equals() method implementation to check if the given two instance are same or not. The hashCode() method is used to determine the bucket in which the element should be placed. See the console out put of the below given example. Even though there are four elements added in the Set, only once the equals() method is called. This means if the elements are coming from different buckets its not necessary to call the equals() method to determine the quality of given objects, they are already different since they have different hashcode. If the hashCode() method returns the same number then the equals() method is called to determine the equality of the given two objects. This implies that two different object can go into same bucket even if the equals() method returns false. In fact all objects can return same hash code and go into same bucket; the only issue is the performance degradation.

Above mentioned behavior is inline with one of the principles declared in the java doc of java.lang.Object.hashCode() method.

“If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.”

package org.sanju.collection;

import java.util.HashSet;
import java.util.Set;

 * @author
 * @date 31st July, 2011
 * Unique Collection in java
 * No duplicate element collection in java
public class UniqueCollectionExample {
	public static void main(String args[]){
		Set employees = new HashSet();
		employees.add(new UniqueCollectionExample().new Employee(1L, "Clean Java"));
		employees.add(new UniqueCollectionExample().new Employee(2L, "Clean Code"));
		employees.add(new UniqueCollectionExample().new Employee(3L, "Java Code"));
		employees.add(new UniqueCollectionExample().new Employee(3L, "Java Clean"));
		System.out.println("The size of collection : "+employees.size());
		for(Employee employee : employees){
	class Employee{
		public Employee(Long id, String name){
			this.empId = id; = name;
		private Long empId;
		private String name;
		public Long getEmpId() {
			return empId;
		public void setEmpId(Long empId) {
			this.empId = empId;
		public String getName() {
			return name;
		public void setName(String name) { = name;
		public int hashCode() {
			return this.empId.intValue(); 
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Employee other = (Employee) obj;
			if (empId == null) {
				if (other.empId != null)
					return false;
			} else if (!empId.equals(other.empId))
				return false;
			return true;
		public String toString(){
			return this.empId +"-";

Console output
/local/opt/java org.sanju.UniqueCollectionExample
The size of collection : 3
1-Clean Java
2-Clean Code
3-Java Code


Written by cleanjava

August 18, 2011 at 1:14 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: