Search code examples
javacomparatorcompareto

java list of objects comparison


I am having List<Student> stud1 = new ArrayList<Student>(); and List<Student> stud2 = new ArrayList<Student>();

And Student class is having members like name, address.

What i have to do is , I have to list the Student from stud1 (if stud1's Student name is equal to stud2's Student name).

How to achieve this ?

I want to know, is there any existing java libraries like ListUtil to solve this problem ?


Solution

  • Well there are many ways to compare elements inside a Collection if you only need to compare the name of the students you can iterate and compare names in both lists, but i think is more elegant to implement Comparable and create your compare_to method in your Student class.

    More info here : http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html