java - Compare 2 lists with different objects to find equals object attribute -
i'm looking elegant , efficient way in java:
public class object1{ string name; int age; } public class object2{ string name; string adress; } list<object1> list1; list<object2> list2;
i want find out every object1 in list1 if there object2 in list2 same name. there better way wrote below?
for (object1 element1 : list1) { (object2 element2 : list2) { if (element2.name.equals(element1.name)){ // stuff } } }
if implement comparator on names in classes could, sort them. having lists sorted, allow binary search on them. each element in object1, binary search in object2.
this make code faster, o(n * m) complexity, o(n * log(m)) complexity.
Comments
Post a Comment