1

I have two list

List<Person> person = new ArrayList<Person>();
List<Employee> employee = new Arraylist<Employee>();

and another list of Person type ,

List<Person> temp_person = new ArrayList<Person>();

Those two list are different , but has one "key" in common that is "id".

Now , I want to check if the "id" of the person is same as "id" of the employee. If I find any match , then add that elements of person to temp_person

asked Nov 4, 2015 at 5:49
3
  • 1
    I believe simple loops will solve your problem. Have you tried any solution? Commented Nov 4, 2015 at 5:54
  • I can solve , but the problem is , person contain more than thousand element , meanwhile employee contain near about 10 element , that's why I do not want to loop through each person . If there is any way , so that I can extract person only match the id of employee Commented Nov 4, 2015 at 6:00
  • Not with ArrayList. - But 10 elements isn't worth creating a HashMap or sorting (for binary search). Just make sure employees is searched in the inner loop. Commented Nov 4, 2015 at 6:03

4 Answers 4

3

The idea here is simple: for all elements contained in person, check to see if a value p.id matches any e.id for all elements contained in employee, and collect them into a new list temp_person.

The result here would be a double-nested loop, or O(nk) efficiency if you couldn't guarantee any relationship between the two entities.

In Java 8, that becomes slightly less painful to read with the Stream API. It takes what I've described above and makes it executable.

final List<Person> temp_person =
 person.stream()
 .filter(p -> employee.stream().anyMatch(e -> e.getId() == p.getId()))
 .collect(Collectors.toList());
answered Nov 4, 2015 at 6:00
1

You have to make two for loops one for loop on the person list and the other to loop on employee list and check if there is any matching (the id's are equal) then add that element to temp_person list

The break inside the loop in order to break that loop and start the other loop (As you already found a match so you need to get another element)

Assuming that you can get your id via getId() and Id is int if it's not int and it is String use equals()

for(int i = 0 ; i < person.size() ; i++){
 for(int j = 0 ; j < employee.size() ; j++){
 if(person.get(i).getId()==employee.get(j).getId()){
 temp_person.add(person.get(i));
 break;
 } 
 }
}
answered Nov 4, 2015 at 5:53
1
  • 1
    We don't know the type of getId(), so take care! Commented Nov 4, 2015 at 5:58
1

You have so few elements that you don't need to worry too much about perf. But for the sake of argument a more effective way for large lists would be as follows: Put persons and empoyees in maps by id (or better yet keep them like this always). Do an intersection between personById.keySet() and employee.keySet() and finally iterate the resulting key set, fetching from the person map and putting into your temp list.

Assuming that the maps are maintained like this and don't have to be computed, this would be a O(1) for the keySets, the intersection would run in O(k) (Guavas) and the copy loop time is relative to the number of matches, worst case O(k), where each copy is O(1) (amortized). So you end up with O(k) time complexity.

answered Nov 4, 2015 at 7:33
-1
It can be done in this manner as below :
List<Person> temp_person = new ArrayList<Person>();
Iterator<Person> firstIterator = firstArrayList.iterator();
while (firstIterator.hasNext()) {
 String str1 = (String) firstIterator.next().ID();
 Iterator<Employee> secondIterator = secondListArrayList.iterator();
 while (secondIterator.hasNext()) {
 String str2 = (String) secondIterator.next().ID();
 if (str1.equals(str2)) {
 temp_person.add(firstIterator.next()); 
 }
 }
}
answered Nov 4, 2015 at 6:50
2
  • Explain what it is you're doing (and why you think using an iterator makes sense in this scenario). Commented Nov 4, 2015 at 6:51
  • Here I am trying to compare two lists by iterating them and then comparing the id's for two different lists which are Person and Employee. If the id's are same then adding the Person object to third list which is temp_person.Pls let me know if anything is wrong. Commented Nov 4, 2015 at 7:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.