找出两列数据的差集_java集合的交集,并集,差集,实际应用场景

apache的轮子 和google的轮子

 <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-collections4artifactId> <version>4.2version> dependency>  <dependency> <groupId>com.google.guavagroupId> <artifactId>guavaartifactId> <version>17.0version> dependency>

测试妹子最喜欢测试互斥操作

什么叫互斥

如:前端下拉框字典 a,b,c,g

这时你停留在前端,马上去后端把数据库字典 a,b,c,g改成a,b,c

g这个元素就无效了,但是前端页面还是提交过来了

后端肯定要过滤掉

集合操作真没必要自己写工具类,还是用大神们的轮子跑,又快又稳定

差集


/**
* 过滤元素
*
* 可怕的互斥操作,测试大大请不要互斥啊
* 场景: 数据库db的字典元素 a,b,c
* 前端过来的from a,g
* (字典有时会被改,前端没有实时联动,
* 导致垃圾数据到后端了,后端必须过滤掉或异常)
* 明显 g这个元素不对
*
* @param db
* @param from
* @return
*/
private static Collection<String> needFilter(Collection<String> db,Collection<String> from){
//from集合减去db集合,就找出垃圾元素了 g
return CollectionUtils.subtract(from,db);
}


/**
*
* 场景:前端from字典 a,b,g
* 需要过滤filter集合 垃圾元素g
* from- filter= a,b
*
*
* @param filter
* @param from
* @return
*/
private static Collection<String> aftertFilter(Collection<String> filter,Collection<String> from){
return CollectionUtils.subtract(from,filter);
}

代码场景案例

去重

前端经常传入重复的元素,后端必须验证

利用CollectionUtils.disjunction轮子轻松搞定

自己写的话就是循环啊,太难看

找出重复的元素

 List fromList = Lists.newArrayList("a","b","c","c");
Set setB = Sets.newHashSet(fromList);
System.out.println(CollectionUtils.disjunction(fromList, setB));

运行结果:[c] (找出了重复的元素)

再也不怕前端哥透传重复元素了,我秒秒钟找出来,哈哈哈哈

交集

List fromList = Lists.newArrayList("a","b","c","c");
Set setB = Sets.newHashSet(fromList);
System.out.println(CollectionUtils.intersection(fromList, setB));

运行结果:[a, b, c] (找出了重复的元素)

比较2个集合是否相等

List fromList = Lists.newArrayList("a","b","c","c");
List fromList2 = Lists.newArrayList("a","c","c","b");
System.out.println(CollectionUtils.isEqualCollection(fromList, fromList2));

运行结果:true

 是否存在交集
List fromList = Lists.newArrayList("a","b","c","c");
List fromList2 = Lists.newArrayList("1","2","3","b");
System.out.println(CollectionUtils.containsAny(fromList, fromList2));

运行结果:true


google的轮子

 HashSet<Integer> setF = Sets.newHashSet(7, 6, 6);
HashSet<Integer> setT = Sets.newHashSet(1, 2, 3,4);
//并集:结果【1,2,3,4,5】
Sets.SetView union = Sets.union(setF, setT);//差集:返回只存在于setF独有的数据, 结果【1,2】Sets.SetView difference = Sets.difference(setF, setT);//交集:结果【3】Sets.SetView intersection = Sets.intersection(setF, setT);if(intersection.size()>0){System.out.println("有交集元素");
}else{System.out.println("无 交集");
}System.out.println("并集:");for (Integer integer : union) {System.out.println(integer);
}System.out.println("差集:");for (Integer integer : difference) {System.out.println(integer);
}System.out.println("交集:");for (Integer integer : intersection) {System.out.println(integer);
}

集合分割器

Listlist= Lists.newArrayList();for(int i=0;i<51;i++){list.add(""+i);
}
Lists.partition(list,10).forEach(lists-> {
System.out.println(lists);
});

运行结果:拆分后的集合

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][10, 11, 12, 13, 14, 15, 16, 17, 18, 19][20, 21, 22, 23, 24, 25, 26, 27, 28, 29][30, 31, 32, 33, 34, 35, 36, 37, 38, 39][40, 41, 42, 43, 44, 45, 46, 47, 48, 49][50]

集合操作熟悉开发代码 快、稳、狠

https://item.jd.com/8358211.html   【京东购买链接】

40d09616700a6f4c9d3f3ed9af4e47bd.png

程序汪往期 精彩回顾

目录:我把精华文章都整理出来了    (目录列的比较全)

给个[在看],是对程序汪最大的支持
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值