Posted In: Java Core, Set

How to remove duplicate objects while adding to Collection – Example1

You have list of objects (in this case users). You want to remove duplicate users depending on some criteria. You could iterate through the list ,check some condition and remove duplicates or you could use little simpler approach using HashSet and overridden equals method as below.

package com.javausecase.hashmap;

import java.io.Serializable;

public class User implements Serializable{
	private static final long serialVersionUID = 398805957846695098L;
	private int age;
	private String firstName;
	private String lastName;

	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (age != other.age)
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "User [age=" + age + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}
}
package com.javausecase.hashmap;

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

public class DuplicateObjects {
	
	public static void main(String[] args) {
		DuplicateObjects dobj = new DuplicateObjects();
		dobj.removeDuplicateUsers();
	}

	private void removeDuplicateUsers() {
		ArrayList<User> userList = createTestData();
		Set<User> userSet = new HashSet<User>();
		userSet.addAll(userList);
		System.out.println(userSet);
	}
	
	private ArrayList<User> createTestData() {
		ArrayList<User> userList = new ArrayList<User>();
		User user1 = new User();
		user1.setAge(35);
		user1.setFirstName("Bat");
		user1.setLastName("Man");
		User user2 = new User();
		user2.setAge(30);
		user2.setFirstName("Super");
		user2.setLastName("Man");
		User user3 = new User();
		user3.setAge(33);
		user3.setFirstName("X");
		user3.setLastName("Men");
		User user4 = new User();
		user4.setAge(35);
		user4.setFirstName("Robin");
		user4.setLastName("Hood");
		User user5 = new User();
		user5.setAge(35);
		user5.setFirstName("Bat");
		user5.setLastName("Man");
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		userList.add(user4);
		userList.add(user5);
		return userList;
	}
}

Result
[User [age=30, firstName=Super, lastName=Man], User [age=35, firstName=Bat, lastName=Man], User [age=33, firstName=X, lastName=Men], User [age=35, firstName=Robin, lastName=Hood]]

Tags: , , , ,

by , on January 25th, 2017

  • Categories