问题
I have two lists of objects. The objects in each list are different subclasses of the same class.
I want to look at objects in list One, check the value of a particular field, and then see if there is/are any objects in list Two that have the same field value. What would be the best way to do this?
回答1:
I believe this is more correct. This is the simplest, easiest solution to implement.
for(TypeA itemA : listA) {
for(TypeB itemB : listB) {
if(itemA.field == ItemB.otherField) {
//Do Whatever
}
}
}
来源:https://stackoverflow.com/questions/11477195/java-list-of-objects-find-objects-with-a-certain-value-in-a-field