2

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 ?

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Muthu
  • 1,550
  • 10
  • 36
  • 62

6 Answers6

3

Just iterate through both of them and check and keep adding in a resultant list If you want to optimize it then

Create a map

Map<String, List<Student>> dictionaryMapOfStudents;

and then select HashMap implementation and look for only those students whose name matches somehow,

For example

A Asdahd, Aasldfalf, Aero
B Baksd, Bajsr, Biro

So now you wont search full list, narrowed down search

jmj
  • 237,923
  • 42
  • 401
  • 438
3

This example may be helps you

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class Repeated {
public static void main( String  [] args ) {
    Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
    Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));

    listOne.retainAll( listTwo );
    System.out.println( listOne );
}

}

Mayank Pandya
  • 1,593
  • 2
  • 17
  • 40
  • Only think you have to consider while doing this is you should have `equals()` overridden for Students to compare objects based on fields such as first name, last name, etc.. – Garbage May 21 '12 at 07:20
2

You can do:

  1. Contruct a HashSet<String> containing all the names of Students in stud2
  2. For each Student in stud1, check if his name is in the set and if it is, add it to the list to return.
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

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

jbossini
  • 71
  • 4
0

Here some algorithm for you:

  • Create Array List for stud3
  • Loop based on stud1 from int i = 0 to i = (stud1.size() - 1)
    • Loop based on stud2 from int j = 0 to j = (stud2.size() - 1)
      • Check if stud1.get(i).getName() equals to stud2.get(j).getName()
        • Check if stud1.get(i) already on stud3
          • continue
        • Else
          • Add stud1.get(i) to stud3
Crazenezz
  • 3,416
  • 6
  • 31
  • 59
  • step 5: after printing name break "for j" loop, or you can print same name few times if stud2 contains it more than once – Pshemo May 18 '12 at 12:47
  • @Pshemo: Thanks for the pointer, I'm using continue when check if there is already the name in the third array. – Crazenezz May 18 '12 at 14:44
0

You can use HashSet for lookup name matching:

class Student {
    private String name;
    private String address;

    public Student(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

public class Program {
    public static void main(String[] args) {
        List<Student> s1 = new ArrayList<Student>();
        List<Student> s2 = new ArrayList<Student>();

        // generate data
        for (int i = 0; i < 5; i++) {
            Student s = new Student("Student" + i, "");
            s1.add(s);
        }
        for (int i = 0; i < 10; i++) {
            Student s = new Student("Student" + i, "");
            s2.add(s);
        }

        // create a lookup HashSet
        Set<String> nameSet = new HashSet<String>();
        for (Student s : s2) {
            nameSet.add(s.getName());
        }

        List<Student> result = new ArrayList<Student>();
        // perform lookup
        for (Student s : s1) {
            if (nameSet.contains(s.getName())) {
                // add the matching student to the result list
                result.add(s);
            }
        }
    }
}
The Tran
  • 400
  • 4
  • 6