aroSuv
aroSuv

Reputation: 137

Java comparing and adding item to list

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

Upvotes: 1

Views: 2380

Answers (4)

Alexander Torstling
Alexander Torstling

Reputation: 18898

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.

Upvotes: 1

arjun99
arjun99

Reputation: 358

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

Upvotes: -1

karim mohsen
karim mohsen

Reputation: 2254

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

Upvotes: 1

Makoto
Makoto

Reputation: 106430

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());

Upvotes: 3

Related Questions