采用java8 lambda表达式 实现java list 交集/并集/差集/去重并集
一般的javaList 交、并集采用简单的 removeAll retainAll 等操作,不过这也破坏了原始的javaList对象,采用java8 lambda表达式流操作则可以不影响原始list对象而得到两个javaList对象的 交、并、差集。
代码语言:javascript复制package com.kaigejava;
import java.util.*;
import static java.util.stream.Collectors.toList;
/**
* @author admin
*/
public class MyTest {
/**
* 用于测试的对象类
*/
static class Student {
/**
* 姓名
*/
private String name;
/**
* 学号 唯一值
*/
private String code;
public Student(String name, String code) {
this.name = name;
this.code = code;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Student)) {
return false;
}
Student student = (Student) o;
return code.equals(student.getCode());
}
@Override
public int hashCode() {
return Objects.hash(code);
}
@Override
public String toString() {
return "Student{"
"name='" name '''
", code='" code '''
'}';
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
}
/**
* 对象类型的处理
*/
public static void showObjectDeal() {
List<Student> list1 = new ArrayList<>();
list1.add(new Student("name1","1001"));
list1.add(new Student("name2","1002"));
list1.add(new Student("name3","1003"));
List<Student> list2 = new ArrayList<>();
list2.add(new Student("name3","1003"));
list2.add(new Student("name4","1004"));
Set<Student> list1Set = new HashSet<>(list1);
Set<Student> list2Set = new HashSet<>(list2);
// 交集
List<Student> intersection = list1.stream().filter(list2Set::contains).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
// 差集 (list1 - list2)
List<Student> reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);
// 差集 (list2 - list1)
List<Student> reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
// 并集
List<Student> listAll = list1.parallelStream().collect(toList());
List<Student> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);
// 去重并集
list1Set.addAll(list2Set);
List<Student> listDistinctAll = new ArrayList<>(list1Set);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
System.out.println("---原来的List1---");
list1.parallelStream().forEach(System.out::println);
System.out.println("---原来的List2---");
list2.parallelStream().forEach(System.out::println);
}
/**
* 简单类型的处理
*/
public static void showSimpleDeal() {
List<String> list1 = new ArrayList<>();
list1.add("1111");
list1.add("2222");
list1.add("3333");
List<String> list2 = new ArrayList<>();
list2.add("3333");
list2.add("4444");
Set<String> list1Set = new HashSet<>(list1);
Set<String> list2Set = new HashSet<>(list2);
// 交集
List<String> intersection = list1.stream().filter(list2Set::contains).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);
// 去重并集
list1Set.addAll(list2Set);
List<String> listDistinctAll = new ArrayList<>(list1Set);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
System.out.println("---原来的List1---");
list1.parallelStream().forEach(System.out::println);
System.out.println("---原来的List2---");
list2.parallelStream().forEach(System.out::println);
}
public static void main(String[] args) {
// 基本类型测试
showSimpleDeal();
// 对象测试
showObjectDeal();
// 一般有filter 操作时,不用并行流parallelStream ,如果用的话可能会导致线程安全问题 !
// parallelStream 由于是并行执行, 输出可能和list本身的顺序不一样 !
}
}