0

I have a Java class as

class Students{
    private String fName;   
    private String lName;
    private String uName;


    public Students(String fName, String lName, String uName) {
            this.fName = fName;
            this.lName = lName;
            this.uName = uName;
     }      

    public String getuName() {
        return uName;
    }
    public void setuName(String uName) {
        this.uName = uName;
    }
    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }

}

Now I have objects created as;

    Students students1 = new Students("har","mat","harmat");
    Students students2 = new Students("pan","son","panson");
    Students students3 = new Students("yogi","jos","yogijos");

    Students students4 = new Students("har","mat","harmat");
    Students students5 = new Students("pan","son","harmat");
    Students students6 = new Students("yogi","jos","someotherUName");
    Students students7 = new Students("yogi","jos","someotherUName2");

Now all these objects get added to combinedList

List combinedList = new ArrayList<SchoolStudents>();

I want to remove duplicate objects from this combinedList based on the following criteria; if fName is same OR if uName is same.

poussma
  • 7,033
  • 3
  • 43
  • 68
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • possible duplicate of [Finding duplicate values in arraylist](http://stackoverflow.com/questions/7281352/finding-duplicate-values-in-arraylist) – Lukas Eder Jul 25 '12 at 14:13
  • And then, more duplicates http://stackoverflow.com/questions/4778260/how-to-remove-arraylist-duplicate-values, http://stackoverflow.com/questions/9962082/prevent-duplicate-entries-in-arraylist, http://stackoverflow.com/questions/5181821/would-like-to-avoid-duplicate-entries-in-a-arraylist, http://stackoverflow.com/questions/5754592/finding-index-of-duplicate-values-in-an-arraylist, http://stackoverflow.com/questions/5503646/find-duplicate-objects-in-an-java-arraylist – Lukas Eder Jul 25 '12 at 14:14
  • homework?? What have you tried so far? hint: use comparable() and maybe Set – maasg Jul 25 '12 at 14:14
  • Why would someone down-vote this? It's a legitimate beginner question. – chris Jul 25 '12 at 14:35

5 Answers5

4

If that is a general criteria to determine if 2 students are equals, you could override equals and hashcode in your class to make 2 students equal based on your criteria.

You can then add the students to a set (typically a HashSet, which will automatically remove the duplicate.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

Store them in a Set (such as a TreeSet) and at construction supply a Comparator to the Set that meets your criteria of fName or uName being the same.

(I'm assuming that the definition of duplicate is external to the students, e.g. it means different things at different times. If it is intrinsic to the student then @assylias answer makes more sense).

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
1

If you need a unique list, I would suggest using anything from the List interface. You would be better of using a Set

Override, hashCode() and equals() method of your Students class

Sujay
  • 6,753
  • 2
  • 30
  • 49
1

Suggest to implement equals() and hashCode() in your Student class and use a HashSet to enforce uniqueness.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0
public List<User> removeDuplicateFromList(List<User> list){
    int s=0;
    List<User> users=new ArrayList<User>();
    for(User us1 :list){
        for(User us2:users){
        if(us1.getIdUser()==us2.getIdUser()){
            s=1;
        }else{
            s=0;
        }

        }   if(s==0){
            users.add(us1);
        }

    }
    return users;
}
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61