@Test
public void ListCoverList() throws Exception {
List<String> listOne = Arrays.asList("1", "2", "3", "4", "5");
List<String> listTwo = Arrays.asList("4", "5", "6", "7", "8");
// 两个集合字符串交集
List<String> intersection = listOne.stream().filter(item -> listTwo.contains(item)).collect(Collectors.toList());
System.out.println("两个集合字符串交集 = " + intersection);
// 两个集合字符串差集 (list1集合删除list1,list2共有的元素)
List<String> reduce1 = listOne.stream().filter(item -> !listTwo.contains(item)).collect(Collectors.toList());
System.out.println("listOne集合删除listOne,listTwo共有的元素后剩余元素 = " + reduce1);
// 两个集合字符串差集 (list2集合删除list1,list2共有的元素)
List<String> reduce2 = listTwo.stream().filter(item -> !listOne.contains(item)).collect(Collectors.toList());
System.out.println("listTwo集合删除listOne,listTwo共有的元素后剩余元素 = " + reduce2);
// 两个集合并集
List<String> listAll = listOne.parallelStream().collect(Collectors.toList());
List<String> listAll2 = listTwo.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("两个集合并集 = " + listAll);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
System.out.println("两个集合去重并集 = " + listAllDistinct);
List<Area> areaListOne = new ArrayList<>();
Area a1 = new Area(1, 10L, "河南", new BigDecimal(100), 1);
Area a2 = new Area(2, 20L, "江西", new BigDecimal(200), 2);
Area a3 = new Area(3, 30L, "北京", new BigDecimal(300), 3);
Area a4 = new Area(4, 40L, "天津", new BigDecimal(400), 1);
areaListOne.add(a4);
areaListOne.add(a1);
areaListOne.add(a2);
areaListOne.add(a3);
List<Area> areaListTwo = new ArrayList<>();
Area a5 = new Area(5, 10L, "巴黎", new BigDecimal(100), 1);
Area a6 = new Area(6, 60L, "伦敦", new BigDecimal(200), 2);
Area a7 = new Area(7, 70L, "新西兰", new BigDecimal(300), 1);
Area a8 = new Area(8, 80L, "悉尼", new BigDecimal(400), 2);
areaListTwo.add(a8);
areaListTwo.add(a5);
areaListTwo.add(a6);
areaListTwo.add(a7);
// 两个对象获取交集
List<Area> objIntersection = areaListOne.stream().filter(item -> areaListTwo.stream().map(e -> e.getUid()).collect(Collectors.toList()).contains(item.getUid())).collect(Collectors.toList());
System.out.println("两个集合字符串交集 = " + objIntersection);
// 两个集合字符串差集 (list1集合删除list1,list2不共有的元素)
List<Area> differenceSet = areaListOne.stream().filter(item -> !areaListTwo.stream().map(e -> e.getUid()).collect(Collectors.toList()).contains(item.getUid())).collect(Collectors.toList());
System.out.println("listOne集合删除listOne,listTwo共有的元素后剩余元素 = " + differenceSet);
// 两个集合并集
List<Area> objlistAll = areaListOne.parallelStream().collect(Collectors.toList());
List<Area> objlist = areaListTwo.parallelStream().collect(Collectors.toList());
objlistAll.addAll(objlist);
System.out.println("两个集合并集 = " + objlistAll);
// 去重
List<Area> uniqueList = objlistAll.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Area::getUid))), ArrayList::new));
System.out.println("两个集合去重并集 = " + uniqueList);
private static List<Object> concat(List<String> s1, String[] s2, Set<String> s3) {
return Stream.of(s1.stream(), Arrays.stream(s2), s3.stream()).flatMap(Function.identity()).collect(Collectors.toList());
}
package com.sysbase.test;
import java.math.BigDecimal;
import java.util.Date;
public class Area {
private Integer id;
private Long uid;
private String name;
private BigDecimal money;
private Date bithday;
private Integer level;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Long getUid() {
return uid;
}
public void setUid(Long uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
public Date getBithday() {
return bithday;
}
public void setBithday(Date bithday) {
this.bithday = bithday;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Area(Integer id, Long uid, String name, BigDecimal money, Integer level, Date bithday) {
this.id = id;
this.uid = uid;
this.name = name;
this.money = money;
this.bithday = bithday;
this.level = level;
}
public Area(Integer id, Long uid, String name, BigDecimal money, Integer level) {
this.id = id;
this.uid = uid;
this.name = name;
this.money = money;
this.level = level;
}
public Area() {
}
}