Collection

1.集合工具类


/**
* 两个集合的差值集合
* @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 ,运行时会报,执行异常!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值