Tuesday 25 June 2013

Removing duplicate data from ArrayList stores List of User objects

1. Create User class and @Override equals method to check duplicate user object

package tts.remove.duplicate;

public class User {
private int userId;
private String userName;
private String firstName;
private String lastName;

public User() {
// TODO Auto-generated constructor stub
}

public User(int id, String username, String fName, String lName) {
this.userId = id;
this.userName = username;
this.firstName = fName;
this.lastName = lName;
}

public int getUserId() {
return userId;
}

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

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

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 boolean equals(Object obj) {
//System.out.println("Equals is calling...");
if (obj.getClass() != getClass()) {
return false;
}
User user = (User) obj;
if (this.userName.equalsIgnoreCase(user.userName)
&& this.userId == user.userId) {
return true;
}
return false;
}

@Override
public int hashCode() {

int hashCode = 31;
hashCode = hashCode + this.userName != null ? this.userName.hashCode() : null;
return hashCode;
}

}

2. Create a class as RemoveDuplicateUsers to remove duplicate data from User objecj

package tts.remove.duplicate;

import java.util.ArrayList;
import java.util.List;

public class RemoveDuplicateUsers {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
userList.add(new User(1, "santoshtts", "Santosh", "Srivastava"));
userList.add(new User(1, "santoshtts", "Santosh", "Srivastava"));
userList.add(new User(2, "dipakdwivedi", "Dipak", "Dwivedi"));
userList.add(new User(2, "dipakdwivedi", "Dipak", "Dwivedi"));
userList.add(new User(3, "umeshyadav", "Umesh", "Yadav"));
userList.add(new User(3, "umeshyadav", "Umesh", "Yadav"));
userList.add(new User(4, "sonumandal", "Sonu", "Mandal"));
userList.add(new User(4, "sonumandal", "Sonu", "Mandal"));
userList.add(new User(5, "binodsoni", "Vinod", "Soni"));
userList.add(new User(5, "binodsoni", "Vinod", "Soni"));
System.out.println("Before removing duplicate list size is: "
+ userList.size() + "\n Data in list ");
List<User> uniqueList = new ArrayList<User>();
System.out.println("----------------------------------------------");
for (User user2 : userList) {
if (!uniqueList.contains(user2)) {
uniqueList.add(user2);

}
System.out.println(user2.getUserId() + " " + user2.getUserName()
+ " " + user2.getFirstName() + " " + user2.getLastName());
}
System.out
.println("_______________________________________________ \n");
System.out.println("After removing duplicate list size is: "
+ uniqueList.size() + "\n Data in list ");
System.out.println("----------------------------------------------");
for (User user : uniqueList) {
System.out.println(user.getUserId() + " " + user.getUserName()
+ " " + user.getFirstName() + " " + user.getLastName());
}

}

}

3. Output

Before removing duplicate list size is: 10
 Data in list 
----------------------------------------------
1 santoshtts Santosh Srivastava
1 santoshtts Santosh Srivastava
2 dipakdwivedi Dipak Dwivedi
2 dipakdwivedi Dipak Dwivedi
3 umeshyadav Umesh Yadav
3 umeshyadav Umesh Yadav
4 sonumandal Sonu Mandal
4 sonumandal Sonu Mandal
5 binodsoni Vinod Soni
5 binodsoni Vinod Soni
_______________________________________________ 

After removing duplicate list size is: 5
 Data in list 
----------------------------------------------
1 santoshtts Santosh Srivastava
2 dipakdwivedi Dipak Dwivedi
3 umeshyadav Umesh Yadav
4 sonumandal Sonu Mandal
5 binodsoni Vinod Soni


Here you can also use  Set<User> uniqueList = new HashSet<User>();  instead  List<User> uniqueList = new ArrayList<User>();

No comments:

Post a Comment