我有兩個列表,其中有不同的對象。檢查一個列表是否包含另一個列表中的元素
List<Object1> list1;
List<Object2> list2;
我要檢查,如果從列表1元素list2中存在,基於特定屬性(Object1和Object2的有(其中包括),一個共同屬性(Long型),命名爲attributeSame)。
現在,我不喜歡這樣寫道:
boolean found = false;
for(Object1 object1 : list1){
for(Object2 object2: list2){
if(object1.getAttributeSame() == object2.getAttributeSame()){
found = true;
//also do something
}
}
if(!found){
//do something
}
found = false;
}
但我認爲這是一個更好更快的方式來做到這一點:) 有人能提出呢?
謝謝!
首先,當您設置found = true;然後簡單地打破;或出來的循環 – Shubhansh 2012-08-03 13:10:42
http://stackoverflow.com/questions/5187888/java-searching-within-a-list-of-objects。此外,爲了迅速搜索嘗試使用二進制搜索,並改變你的DS套件的情況... – Shubhansh 2012-08-03 13:13:14
他們是否共享除了對象共同的父母? – Woot4Moo 2012-08-03 13:16:59