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
-
1I believe simple loops will solve your problem. Have you tried any solution?Chetan Gole– Chetan Gole2015年11月04日 05:54:51 +00:00Commented 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 employeearoSuv– aroSuv2015年11月04日 06:00:36 +00:00Commented 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.laune– laune2015年11月04日 06:03:54 +00:00Commented Nov 4, 2015 at 6:03
4 Answers 4
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());
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;
}
}
}
-
1We don't know the type of
getId()
, so take care!laune– laune2015年11月04日 05:58:21 +00:00Commented Nov 4, 2015 at 5:58
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.
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());
}
}
}
-
Explain what it is you're doing (and why you think using an iterator makes sense in this scenario).Makoto– Makoto2015年11月04日 06:51:47 +00:00Commented 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.arjun99– arjun992015年11月04日 07:02:04 +00:00Commented Nov 4, 2015 at 7:02