现在程序有一个校验机制
List list1=new ArrayList();
list1.add("1");
list1.add("2");
list1.add("3");
List list2=new ArrayList();
list2.add("3");
list2.add("4");
现在要找出list1中的值在list2中不存在,并且找出list2中的值在list1中哪些不存在
如:我要找到list2中在list1中的值3,list1中在list2中不存在的值1,4现在我的思路是两个for 循环
for(int i=0 ;i<list1.size();i++){
if(list2.contains(list1.get(i))){
...
}else{
...
}
} for(int i=0 ;i<list2.size();i++){
if(list1.contains(list2.get(i))){
...
}else{
...
}
}这样写貌似性能比较差,请问高手更好的算法,感激不尽
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html
中intersection方法。源代码可以下载。
for (PurviewBean purviewBean : purviewList) {
map.put(purviewBean.getMenuId()+purviewBean.getOperateId(), purviewBean);
}
for (PurviewBean purviewBean : menuOperateList) {
PurviewBean a = (PurviewBean) map.get(purviewBean.getMenuId()+purviewBean.getOperateId());
if(a!=null){
purviewBean.setIsSelected(1);
}
}
list1就只有3了取补集:list1.removeAll(list2);
list1就只有chinese wholesalrs、2了
import java.util.Collections;
List list1=new ArrayList();
list1.add("1");
list1.add("2");
list1.add("3");
List list2=new ArrayList();
list2.add("3");
list2.add("4");Collection<String> intersection = CollectionUtils. disjunction(list1, list2);
进行排序之后,再进行对比 速度就会快很多。
应该:list1中在list2中不存在的值1,2