// 差集 (list1 - list2 = list1 中不同数据)
List<Person> reduce1 = list1.stream().filter(
a -> !list2.stream().map(
b -> b.getAge() + "&" + b.getName()).collect(Collectors.toList())
.contains(a.getAge() + "&" + a.getName())).collect(Collectors.toList());
reduce1.parallelStream().forEach(System.out :: println);
System.out.println("=============================得到差集 reduce2 (list2 - list1)==================================");
List<Person> reduce2 = list2.stream().filter(
a -> !list1.stream().map(
b -> b.getAge() + "&" + b.getName()).collect(Collectors.toList()).contains(a.getAge() + "&" + a.getName()))
.collect(Collectors.toList());
reduce2.parallelStream().forEach(System.out :: println);
// List1与 List2的交集
// 过滤list1 ,遍历集合list2
// anyMatch(Predicate p) 传入一个断言型函数,对流中所有的元素进行判断,只要有一个满足条件返回true,不满足false。
// 取出list2中,符合判断条件的元素,返回值为true。将对象保存新集合中。
List<Person> intersectA = list1.stream().filter(
a -> list2.stream().map(
Person::getName).anyMatch( // 流中是否有元素能匹配给定的谓词,有则返回true 否false。
name -> Objects.equals(a.getName(), name)))
.collect(Collectors.toList());
// 遍历List
intersectA.parallelStream().forEach(System.out :: println);
// List对象数据去重
Map<Object, Boolean> map = new HashMap<>();
List<Person> list8 = list1.stream()
.filter(i -> map.putIfAbsent(i.getName()+"&"+i.getAge(), Boolean.TRUE) == null).collect(Collectors.toList());
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Person {
private String name;
private Integer age;
private Integer weight;
}
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = bb.class)
public class test {
@Test
public void testSelectUser(){
// list 1
Person p1 = new Person("张三",null,50); // a
Person p2 = new Person("张三",26,60); // a
Person p3 = new Person("李四",null,70); // a
Person p4 = new Person("李四",110,60); // a
Person p5 = new Person(null,null,60); // a
Person p6 = new Person(null,null,70); // a
Person p11 = new Person("张三",25,50);
Person p12 = new Person("张三",25,80);
Person p13 = new Person("李5",110,60);
Person p14 = new Person("李6",110,60);
// List 2
Person p21 = new Person("李四",null,22); // b
Person p22 = new Person("李四",110,11); // b
Person p23 = new Person("张三",26,11); // b
Person p24 = new Person("张三",null,11); // b
Person p25 = new Person(null,null,80); // b
Person p31 = new Person("张三",20,5);
Person p32 = new Person("李四",119,11);
List<Person> list1 = new ArrayList<>();
list1.add(p1);
list1.add(p2);
list1.add(p3);
list1.add(p4);
list1.add(p5);
list1.add(p6);
list1.add(p11);
list1.add(p12);
list1.add(p13);
list1.add(p14);
List<Person> list2 = new ArrayList<>();
list2.add(p21);
list2.add(p22);
list2.add(p23);
list2.add(p24);
list2.add(p25);
list2.add(p31);
list2.add(p32);
list2.add(p5);
}
}
************************************得到差集 reduce1 (list1 - list2 = list1)***********************************
Person{name='李5', age=110, weight=60}
Person{name='李6', age=110, weight=60}
Person{name='张三', age=25, weight=50}
Person{name='张三', age=25, weight=80}
=============================得到差集 reduce2 (list2 - list1)==================================
Person{name='李四', age=119, weight=11}
Person{name='张三', age=20, weight=5}
=============================得到交集==================================
Person{name='null', age=null, weight=70}
Person{name='null', age=null, weight=60}
Person{name='张三', age=null, weight=50}
Person{name='张三', age=25, weight=80}
Person{name='张三', age=25, weight=50}
Person{name='李四', age=null, weight=70}
Person{name='张三', age=26, weight=60}
Person{name='李四', age=110, weight=60}
=======================================================================
引用地址:点击跳转