对比两组List(可查交集、全集、差集)

对比两组List(可查交集、全集、差集)

/**
* 对比两个List
*
* @param l1 
* @param l2 
* @return
*/
public static Collection second(List l1, List l2) {
   // 创建集合
   Collection realA = new ArrayList<String>(l1);
   Collection realB = new ArrayList(l2);
   // 求交集
   realA.retainAll(realB);
   //System.out.println("交集结果:" + realA);
   Set result = new HashSet();
   // 求全集
   result.addAll(l1);
   result.addAll(l2);
   //System.out.println("全集结果:" + result);
   // 求差集:结果
   Collection aa = new ArrayList(realA);
   Collection bb = new ArrayList(result);
   bb.removeAll(aa);
   //System.out.println("最终结果:" + bb);
   return bb;
}

public static void main(String[] args) {

	List l1 = new ArrayList();
	l1.add(1);
	l1.add(2);
	l1.add(3);
    List l2 = new ArrayList();	
    l2.add(2);
    l2.add(3);
	/**
	* 调用对比方法
	*/
	Collection IdNumberLis = second(l1, l2);
	//取出对比结果
	Iterator iterator = IdNumberLis.iterator();
	if (iterator != null) {
	   while (iterator.hasNext()) {
	       Object object = iterator.next();
	       if (object != null) {
	           System.out.println(object);
	       }
	   }
	}
}   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值