Java - List of objects. Find object(s) with a certain value in a field

拜拜、爱过 提交于 2019-12-08 08:50:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!