Posted In: Java Core, Set

How to remove duplicate objects while adding to Collection and Sort – Example2

Use case – We have list of objects (in this case users). We want to remove duplicate users depending on some criteria. Then sort them. In this example with first name. Both of this can be achieved by overriding equals and Comparable.compareTo methods.

package com.javausecase.set1;

import java.io.Serializable;

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

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	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 int compareTo(User user) {
		return this.getFirstName().compareTo(user.getFirstName());
	}

	@Override
	public String toString() {
		return "User [age=" + age + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}
}

package com.javausecase.set1;

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

public class RemoveDuplicateAndSort {

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

	private void removeDuplicateUsers() {
		List<User> userList = createTestData();
		Set<User> userSet = new HashSet<User>();
		userSet.addAll(userList);
		userList.clear();
		userList.addAll(userSet);
		Collections.sort(userList);
		System.out.println(userList);
	}

	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=35, firstName=Bat, lastName=Man], User [age=35, firstName=Robin, lastName=Hood], User [age=30, firstName=Super, lastName=Man], User [age=33, firstName=X, lastName=Men]]

Tags: , , , , ,

by , on January 30th, 2017

  • Categories