Home > Java > javaTutorial > How to filter the same values ​​​​and filter different values ​​​​in a list in java

How to filter the same values ​​​​and filter different values ​​​​in a list in java

王林
Release: 2023-04-18 09:45:31
forward
1397 people have browsed it

The code is as follows:

public class People {
    private String id;
    private String somethingElse;

    public People() {
    }

    public People(String id, String somethingElse) {
        this.id = id;
        this.somethingElse = somethingElse;
    }

    public String getId() {
        return id;
    }

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

    public String getSomethingElse() {
        return somethingElse;
    }

    public void setSomethingElse(String somethingElse) {
        this.somethingElse = somethingElse;
    }

    @Override
    public String toString() {
        return "People{" +
                "id='" + id + '\'' +
                ", somethingElse='" + somethingElse + '\'' +
                '}';
    }
}
Copy after login

people entity class and student entity class
People class

student
public class Student {
    private String id;
    private String idCard;
    private String somethingElse;

    public Student() {
    }

    public Student(String id, String idCard, String somethingElse) {
        this.id = id;
        this.idCard = idCard;
        this.somethingElse = somethingElse;
    }

    public String getId() {
        return id;
    }

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

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getSomethingElse() {
        return somethingElse;
    }

    public void setSomethingElse(String somethingElse) {
        this.somethingElse = somethingElse;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", idCard='" + idCard + '\'' +
                ", somethingElse='" + somethingElse + '\'' +
                '}';
    }
}
Copy after login

Test class

public static void main(String[] args) throws Exception{
//初始化数据
        List<Student> studentList = new ArrayList(){{
               add(new Student("1", "11", "111")) ;
               add(new Student("2", "22", "222"));
               add(new Student("3", "33", "333"));
        }};
        List<People> peopleList = new ArrayList(){
            {
                add(new People("11", "111"));
                add(new People("222", "222"));
                add(new People("33", "333"));
        }};
        //获取相同字段内容,转化为set
        Set<String> ids = peopleList
                .stream()
                .map(People::getId)
                .collect(Collectors.toSet());//过滤重复内容
        List<Student> result = studentList
                .stream()
                .filter(e -> ids.contains(e.getIdCard()))
                .collect(Collectors.toList());
        System.out.println(result);
         //获取相同字段内容,转化为set
        Set<String> ids = peopleList
                .stream()
                .map(People::getId)
                .collect(Collectors.toSet());//过滤重复内容
        List<Student> result = studentList
                .stream()
                .filter(e -> !ids.contains(e.getIdCard()))
                .collect(Collectors.toList());
        System.out.println(result);
}
Copy after login

The above is the detailed content of How to filter the same values ​​​​and filter different values ​​​​in a list in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template