一、前言

在日常的开发工作中,我们经常会遇到需要对两个或多个List集合进行操作的需求,比如求两个列表的交集、并集、差集以及去重后的并集。这些操作在Java 8中变得非常简单,借助于流(Stream)API和集合类的新特性,我们可以轻松实现。下面将通过示例代码来展示如何使用Java 8来完成这些任务。

二、项目实战

1.创建一个实体类

我们定义一个普通的实体类作为我们的数据类型。

package com.example.springbootdemo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {

    /**
     * 名字
     */
    private String name;
    /**
     * 年龄
     */
    private int age;
    /**
     * 年级
     */
    private String grade;

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

2.创建两个获取数据集合方法

package com.example.springbootdemo.util;

import com.example.springbootdemo.domain.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentUtil {


    public static List<Student> getStudentList1() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("aa", 20, "大一"));
        list.add(new Student("bb", 21, "大二"));
        list.add(new Student("cc", 23, "大三"));
        list.add(new Student("dd", 24, "大四"));
        list.add(new Student("ee", 21, "大二"));
        return list;
    }

    public static List<Student> getStudentList2() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("qx", 21, "大一"));
        list.add(new Student("bb", 21, "大二"));
        list.add(new Student("dd", 24, "大四"));
        list.add(new Student("cja", 24, "大四"));
        return list;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

3.求交集

要找出两个列表中的共同元素,可以使用stream()结合filter()方法,并且用collect(Collectors.toList())收集结果:

package com.example.springbootdemo.test;

import com.example.springbootdemo.domain.Student;
import com.example.springbootdemo.util.StudentUtil;

import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<Student> studentList1 = StudentUtil.getStudentList1();
        List<Student> studentList2 = StudentUtil.getStudentList2();
        List<Student> list = studentList1.stream().filter(s -> studentList2.contains(s)).collect(Collectors.toList());
        System.out.println(list);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

运行结果:

Java8处理对象List集合之间的交集、并集、差集和去重_去重

4.求并集

合并两个列表可以使用Stream.concat()方法:

package com.example.springbootdemo.test;

import com.example.springbootdemo.domain.Student;
import com.example.springbootdemo.util.StudentUtil;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {
    public static void main(String[] args) {
        List<Student> studentList1 = StudentUtil.getStudentList1();
        List<Student> studentList2 = StudentUtil.getStudentList2();
        //合并 去重
        List<Student> list = Stream.concat(studentList1.stream(), studentList2.stream()).distinct().collect(Collectors.toList());
        System.out.println(list);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

运行结果:

[Student(name=aa, age=20, grade=大一), Student(name=bb, age=21, grade=大二), Student(name=cc, age=23, grade=大三), Student(name=dd, age=24, grade=大四), Student(name=ee, age=21, grade=大二), Student(name=qx, age=21, grade=大一), Student(name=cja, age=24, grade=大四)]
  • 1.

4.求差集

找出在第一个列表中但不在第二个列表中的元素:

package com.example.springbootdemo.test;

import com.example.springbootdemo.domain.Student;
import com.example.springbootdemo.util.StudentUtil;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {
    public static void main(String[] args) {
        List<Student> studentList1 = StudentUtil.getStudentList1();
        List<Student> studentList2 = StudentUtil.getStudentList2();
        List<Student> list = studentList1.stream().filter(s -> !studentList2.contains(s)).collect(Collectors.toList());
        System.out.println(list);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

运行结果:

Java8处理对象List集合之间的交集、并集、差集和去重_去重_02

三、总结
通过上述方法,我们可以很方便地在Java 8中处理集合的交集、并集、差集以及去重并集问题。这对于数据处理和逻辑实现提供了很大的便利。当然,在实际项目中,根据具体需求可能还需要考虑性能优化等问题。