0

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

Jijo Mathew
  • 222
  • 1
  • 3
  • 9

1 Answers1

0

THE RESULT YOU POSTED IS VALID

inside your comperator you check:

  1. name
  2. location
  3. score

that is why you are getting duplicates - because of difference in scores.. you have just swaped age and score parameters that's all

Student(String sName,String loc,int scr,int ag,String dep)

Student s1= new Student("John","Aus",25,100,"Finance");
ymz
  • 6,602
  • 1
  • 20
  • 39
  • Thanks ymz. As you mentioned it was my mistake in the code. Just updated it and works fine. Just one more clarification while doing such comparison does the order of the elements in the list matters? – Jijo Mathew Nov 28 '14 at 02:24
  • the order of the element in the list has no effect over the result (will stay the same).. as for **performance** - that is another question i'm afraid :) – ymz Nov 28 '14 at 02:30
  • lol :) here is something interesting for you: http://stackoverflow.com/questions/1463284/hashset-vs-treeset – ymz Nov 28 '14 at 02:41
  • Thanks I will check that url... Mean time I just added 3 more Objects to the list as below, but now the final list contains duplicates... So seems like it is not working.... Student s7= new Student("Jim","Ind",28,102,"Marketing"); Student s8= new Student("Jim","Ind",28,102,"Marketing"); Student s9= new Student("Ants","Ind",29,102,"Marketing"); – Jijo Mathew Nov 28 '14 at 02:55