1.集合工具类
备注:拷贝集合必须赋值,(size+1)只是制定了该集合的初始化大小,而集合本身还是空
若不初始化,会报异常
2.集合的remove与迭代类的remove
运行:200
实际结果应为300
解释:
[table]
||100||100||200||100||
||0||1||2||3||
[/table]
但Iterator 运行到 下标 2 时 ,结合remove 掉 200
集合变为:
[table]
||100||100||100||
||0||1||2||
[/table]
此时Iterator的位置依然在 2 处,因为是 Collection 进行的删除操作,Iterator 不知道
继续判断 hasNext() = false ,丢失数据
结果:300
解释:
迭代器的remove方法
首先调用collection.remove方法
然后将当前所处位置前移一位,这样就能保证数据不丢失
备注:
若上述代码中的Collection换为List ,运行时会报,执行异常!
/**
* 两个集合的差值集合
* @param sourceList
* @param destinyList
* @return sourceList.removeAll(destinyList)
*/
public static List<Integer> differListInteger(List<Integer> sourceList,List<Integer> destinyList){
if(CollectionUtils.isEmpty(sourceList))
return null ;
if(CollectionUtils.isEmpty(destinyList))
return sourceList;
List<Integer> differList = initDestinyList(sourceList.size());
Collections.copy(differList, sourceList);
differList.removeAll(destinyList);
return differList;
}
/**
* 两个集合的交集集合
* @param sourceList
* @param destinyList
* @return
*/
public static List<Integer> intersectListInteger(List<Integer> sourceList,List<Integer> destinyList){
if(CollectionUtils.isEmpty(sourceList))
return null ;
if(CollectionUtils.isEmpty(destinyList))
return sourceList;
List<Integer> intersectList = initDestinyList(sourceList.size());
Collections.copy(intersectList, sourceList);
intersectList.retainAll(destinyList);
return intersectList;
}
/**
* 两个集合的并集集合
* @param sourceList
* @param destinyList
* @return
*/
public static List<Integer> unionListInteger(List<Integer> sourceList,List<Integer> destinyList){
if(CollectionUtils.isEmpty(sourceList))
return null ;
if(CollectionUtils.isEmpty(destinyList))
return sourceList;
List<Integer> unionList = initDestinyList(sourceList.size());
Collections.copy(unionList, sourceList);
unionList.removeAll(destinyList);
unionList.addAll(destinyList);
return unionList;
}
/**
* 初始化目标集合
* @param sourceListSize
* @return
*/
public static List<Integer> initDestinyList(int sourceListSize){
List<Integer> differList = new ArrayList<Integer>(2*sourceListSize+1);
for(Integer index = 0 ; index <= sourceListSize ; index ++){
differList.add(null);
}
return differList ;
}
备注:拷贝集合必须赋值,(size+1)只是制定了该集合的初始化大小,而集合本身还是空
若不初始化,会报异常
2.集合的remove与迭代类的remove
public static void main(String[] args) {
Collection<Integer> list = new ArrayList<Integer>(5);
list.add(100);
list.add(100);
list.add(200);
list.add(100);
Iterator<Integer> i = list.iterator();
Integer s = 0 ;
while(i.hasNext()){
Integer num = i.next() ;
if(num.equals(100)){
s = s + num ;
}else{
list.remove(200);
}
}
System.out.println(s);
}
运行:200
实际结果应为300
解释:
[table]
||100||100||200||100||
||0||1||2||3||
[/table]
但Iterator 运行到 下标 2 时 ,结合remove 掉 200
集合变为:
[table]
||100||100||100||
||0||1||2||
[/table]
此时Iterator的位置依然在 2 处,因为是 Collection 进行的删除操作,Iterator 不知道
继续判断 hasNext() = false ,丢失数据
public static void main(String[] args) {
Collection<Integer> list = new ArrayList<Integer>(5);
list.add(100);
list.add(100);
list.add(200);
list.add(100);
Iterator<Integer> i = list.iterator();
Integer s = 0 ;
while(i.hasNext()){
Integer num = i.next() ;
if(num.equals(100)){
s = s + num ;
}else{
i.remove();
}
}
System.out.println(s);
}
结果:300
解释:
迭代器的remove方法
首先调用collection.remove方法
然后将当前所处位置前移一位,这样就能保证数据不丢失
备注:
若上述代码中的Collection换为List ,运行时会报,执行异常!