List求交集和并集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihua5419/article/details/86623201
	public static void main(String[] args) {
	 
		 List<String> str1=new ArrayList<>();
		 str1.add("aaa");
		 str1.add("bbb");
		 str1.add("ccc");

		 List<String> str2 = new ArrayList();
		 str2.add("bbb");
		 str2.add("ccc");
		 str2.add("ddd");
		 str2.add("eeee");
	     
		  System.out.println("*****************交集结果如下********************");
	     //交集
	     List<String> intersectionList = str1.stream().filter(item -> str2.contains(item)).collect(Collectors.toList());
	     intersectionList.parallelStream().forEach(System.out :: println);
//	     intersectionList.stream().forEach(System.out :: println);
	     System.out.println("******************并集结果如下*******************");
	     //并集
	     List<String> unionSet=new ArrayList<>();
	     unionSet.addAll(str1);
	     unionSet.addAll(str2);
	     unionSet.stream().forEach(System.out :: println);
	     System.out.println("******************并集结果再去重如下*******************");
	     //并集去重
	     unionSet.stream().distinct().forEach(System.out :: println);
	     
	}
*****************交集结果如下********************
ccc
bbb
******************并集结果如下*******************
aaa
bbb
ccc
bbb
ccc
ddd
eeee
******************并集结果再去重如下*******************
aaa
bbb
ccc
ddd
eeee

 关于StreamparallelStream的区别此篇不再做详解,下一篇单独学习分析

微信扫描下方二维码(新开通的个人微信公众号)  更多优质资源及优质文章及时获取 请大家多多支持哦

猜你喜欢

转载自blog.csdn.net/lihua5419/article/details/86623201
今日推荐