I have an array list with a number of objects. I want to remove duplicate Objects from it.. I tried the below option with TreeSet and Comparator but it is not working. Following class objects are added in the list
public class Student {
private String name;
private String location;
private int score;
private int age;
private String department;
Student(){}
Student(String sName,String loc,int ag,int scr,String dep){
setName(sName);
setLocation(loc);
setScore(scr);
setAge(ag);
setDepartment(dep);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
Below is my main class
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class MyMain {
public static void main(String[] args) {
Student s1= new Student("John","Aus",25,100,"Finance");
Student s2= new Student("John","Aus",25,100,"Finance");
Student s3= new Student("John","Aus",26,100,"Finance");
Student s4= new Student("Alex","Ind",20,101,"Finance");
Student s5= new Student("Alex","Ind",20,101,"Finance");
Student s6= new Student("Alex","Ind",28,101,"Finance");
ArrayList<Student> studentsList= new ArrayList<Student>();
studentsList.add(s1);
studentsList.add(s2);
studentsList.add(s3);
studentsList.add(s4);
studentsList.add(s5);
studentsList.add(s6);
for(int i=0;i<studentsList.size();i++){
Student s=(Student)studentsList.get(i);
System.out.println(i+ " "+s.getName()+" "+s.getLocation()+" "+s.getAge()+" "+s.getScore()+" "+s.getDepartment());
}
Set set = new TreeSet(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
if( s1.getName().equalsIgnoreCase(s2.getName()) && s1.getLocation().equalsIgnoreCase(s2.getLocation()) && s1.getScore()==s2.getScore()){
return 0;
}
return 1;
}
});
set.addAll(studentsList);
studentsList.clear();
studentsList.addAll(set);
System.out.println("---------Final Result----------");
for(int i=0;i<studentsList.size();i++){
Student s=(Student)studentsList.get(i);
System.out.println(i+ " "+s.getName()+" "+s.getLocation()+" "+s.getAge()+" "+s.getScore()+" "+s.getDepartment());
}
}
}
I want to remove Objects from the list which contains same 'Name','Location' and 'Score'
I am getting the following output while running the program
0 John Aus 100 25 Finance
1 John Aus 100 26 Finance
2 Alex Ind 101 20 Finance
3 Alex Ind 101 28 Finance
But my expected output is
0 John Aus 100 25 Finance
1 Alex Ind 101 20 Finance
Please help me on this. Is this approach correct? Also I would like to keep the Student class intact ( can't override equals and hashCode)
Please advice