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;
    }
    
}

Also I call this using

public class TestClass {
public static void main(String[] args) {

    Students students1 = new Students("xyz","abc","xyzAbc");
    Students students2 = new Students("poi","son","poison");
    Students students3 = new Students("yog","jos","yogjos");
    
    Students students4 = new Students("xyz","abc","xyzAbc");
    Students students5 = new Students("pon","son","xyzAbc");
    Students students6 = new Students("yog","jos","someotherUName");
    Students students7 = new Students("yog","jos","someotherUName2");
    
    List studentList1 = new ArrayList();
    List studentList2 = new ArrayList();
    
    studentList1.add(students1);
    studentList1.add(students2);
    studentList1.add(students3);
    
    studentList2.add(students4);
    studentList2.add(students5);
    studentList2.add(students6);
}
}

Now I want a filtered list which would contain only unique "uName" values. Thus I want the comparison between "uName" field of each list and remove common ones.

At the end I would want 2 filtered list for studentList1 and studentList2.

I read about the removeAll method, but it seems to work with List of Integer/String data and not with List of Objects (as in my case).

1

7 Answers 7

3

You can put your list to set and then (if you need) to take it back:

new ArrayList(new HashSet<String>(list)) creates list that contains only unique elements from source list.

Sign up to request clarification or add additional context in comments.

Comments

3

1. If you want each list to have unique uName value, then you can use TreeSet from java.util.Collection along with Interface Comparator from java.util.Comparator.

2. If you want to merge both the list and have unique uName, then combine both the list and then use TreeSet and Comparator.

3. Comparator gives the flexibility to compare in more than one way...

2 Comments

Thx a lot...Could you please provide an example for the 2nd option (merging both list with only unique uName) ?
Merge both the list first...... then insert it in a TreeSet which has used a Comparator that compares the students with their uName... See these example about the TreeSet with the Comparator.. javaexamples4u.com/2009/04/treeset-comparator.html ... I am at work now... when i will reach home, i will try to reply back
2

You can still use removeAll if the Objects in the List implement equals() properly.

AbstractCollection, which is the base for most kind of List implementations (including ArrayList) uses contains() in its implementation of removeAll. ArrayList's implementation of contains relies on indexOf(), which lastly uses equals().

You could implement equals() in your Student class to specify that an Student is equal to another if and only their uName fields are equal.

Please note that equals has associated semantics (see its javadoc), and you should be careful when choosing how to implement it. Consider if two student instances really represent the same student when their uNames are equal. In my opinion, this sounds like a very specific requirement of how to sort these things out and should not impact the semantics of the class.

You'll be much better off with @AlexR or @KumarVivekMitra's approach.

1 Comment

Could you please provide an example of the same ? Also in my case, I want the comparision between the ivars of the Students object.
2

Firstly, you should be typing your lists:

List<Students> studentList1 = new ArrayList<Students>();

Secondly, implement hashCode() and equals() on your Students class that both delegate to uName:

public boolean equals(Object o) {
    return o instanceof Students && ((Students)o).uName.equals(uName);
}

public int hashCode() {
    return uName.hashCode();
}

Now removeAll() will work just fine.


Another option is to use Set, which only allows unique values as determined by the equals() method. If you add the above methods to your class, you could just do this:

Set<Students> students = new HashSet<Students>();

then add what you like to it and there will only ever be unique uName students in it.


btw, you should name your class in the singular - ie Student not Students.

2 Comments

Thx a lot for your help...How exactly do I call the removeAll() method ?
It's all in the javadoc, but it's simply list1.removeAll(list2);
0

You could also try using the apache commons CollectionUtils.disjunction. (link: http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html#disjunction(java.util.Collection, java.util.Collection))

Comments

0

Your class Student should override methods hashcode and equals, so youy object can be unique by name. Here you can read how to do that.

Also note that all objects which are maintained in some collection should have those methods overriden, so you always know how are they compared.

In next step you can use removeAll method or the solution with Set, it's up to you :-)

Set example

List<String> list = new ArrayList(new HashSet<String>(studentList)); // here is your unique list

Or you could use custom Comparator instead of overriding hashcode and equals but if you want your students always unique, you should override them

Comments

0

A full working solution could be as follows.

Class Student{


.....
......
.....

   public boolean equals(Object o) {
       return o instanceof Students && ((Students)o).uName.equals(uName);
   }

   public int hashCode() {
       return uName.hashCode();
   }




}


Set studentList1 = new hashSet();
Set studentList2 = new hashSet();

Put your elements in these Sets.

Also if you want unique non-matching elements in both Sets. then write as follows.

studentList1.removeAll(studentList2 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.