//List转换为逗号分隔的字符串
List<Integer> list = Stream.of(1, 2, 3).collect(Collectors.toList());//jdk8 初始化List
String result=StringUtils.join(list .toArray(), ",");//list转换为逗号分隔的字符串
System.out.println("【List转换为逗号分隔字符串】:"+result);//1,2,3
//List取交集、差集、合集
List<Integer> list1 = Stream.of(1,2,3,6).collect(Collectors.toList());//jdk8 初始化List
List<Integer> list2 = Stream.of(1,2,3,7,9).collect(Collectors.toList());//jdk8 初始化List
// List差集 (list1 - list2 取出list1在list2中没有的元素)
List<Integer> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("【差集 reduce1 (list1 - list2)】:"+reduce1);//[6]
// List差集 (list2 - list1 取出list2在list1中没有的元素)
List<Integer> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("【差集 reduce2 (list2 - list1)】:"+reduce2);//[7, 9]
// List交集
List<Integer> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("【交集 intersection】:"+intersection);//[1, 2, 3]
// List合集(合并去重)
list1.addAll(list2);//合并
List newList = list1.stream().distinct().collect(Collectors.toList());//jdk8去重
System.out.println("【合并后去重result】:"+newList);//[1, 2, 3, 6, 7, 9]
来源:CSDN
作者:Fantasy_IT
链接:https://blog.csdn.net/FANTASY522272820/article/details/103486703