1

I have two different List objects and I want to get a merged List of two objects based on a common attribute of those two objects and the new list should contain only the common objects. The size of both objects also varies. My first object:

ObjectA(distance,remainingtime,msg1_received_time)
ObjectB(remainingtime,msg1_decoding_time,phase)

And I want to have a List with values where only the remainingtime is same in both cases.Can someone please guide me?

My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
java_learner
  • 182
  • 3
  • 13

1 Answers1

1

If you don't have any tieme constraints you can always iterate on the elements of one list and then check if they exist on the elements of the other. An algorithm of magnitude O(n^2):

for(ObjectA el : listOfA)
{
    for(ObjectB in : listOfB)
    {
        if(el.remainingtime == in.remainingtime)
        {
            resultList.add(el);
            break;
        }
    }
}

If you are looking for something a bit more efficient then you can try using Set<T>, which should make the accesses much faster.

cgledezma
  • 612
  • 6
  • 11