两个List合并为一个List,且去重。(利用java8-Stream流)

本文介绍了Java中的Employee类定义,包括属性和构造函数,以及如何使用EmployeeData工具类生成数据。测试部分展示了如何过滤年龄并合并数据,使用了StreamAPI的distinct方法去除重复元素。
摘要由CSDN通过智能技术生成

基本类Employee

package com.neuedu.java2;

public class Employee {

    private int id;
    private String name;
    private int age;
    private double salary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee() {
        System.out.println("Employee().....");
    }

    public Employee(int id) {
        this.id = id;
        System.out.println("Employee(int id).....");
    }

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Employee(int id, String name, int age, double salary) {

        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        Employee employee = (Employee) o;

        if (id != employee.id)
            return false;
        if (age != employee.age)
            return false;
        if (Double.compare(employee.salary, salary) != 0)
            return false;
        return name != null ? name.equals(employee.name) : employee.name == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + age;
        temp = Double.doubleToLongBits(salary);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}

造数据工具类

package com.neuedu.java2;

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

/**
 * 造数据
 */
public class EmployeeData {
    public static List<Employee> getEmployees(){
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1,"马哥",34,11111));
        list.add(new Employee(2,"里哥大王",13,2222));
        list.add(new Employee(3,"刘哥",18,33333));
        list.add(new Employee(4,"等哥大拿",99,4444));
        list.add(new Employee(5,"啊哥",55,555));
        return list;
    }
}

正文开始测试

package com.neuedu.testListMerage;

import com.neuedu.java2.Employee;
import com.neuedu.java2.EmployeeData;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo01 {
    @Test
    public void test01(){
        //创造数据
        List<Employee> employees = EmployeeData.getEmployees();
        //过滤出年龄为55和34的数据
        List<Employee> collect = employees.stream().filter(employee -> employee.getAge() == 55 || employee.getAge() == 34)
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
        //将过滤后的数据 与 源数据进行合并
        //注意:Stream.of(collect, employees) 以第一个参数为基准,去合并
        List<Employee> list = Stream.of(collect, employees).flatMap(Collection::stream).distinct().collect(Collectors.toList());
        System.out.println("_________________________");
        list.forEach(System.out::println);
    }
}

运行效果

distinct去重

flatMap将每个元素换成流,合成新的流。(联想记忆:addAll方法)

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值