Use streams in Java to filter two collections according to a certain field?

Use streams in Java to filter two collections according to a certain field?

In Java, you can use Stream to filter and deduplicate two collections. The following is a sample code showing how to filter and deduplicate based on a field of an object:

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

public class Main {
    public static void main(String[] args) {
        // 创建一个对象集合
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Alice", 25));
        persons.add(new Person("Bob", 30));
        persons.add(new Person("Alice", 25));
        persons.add(new Person("Charlie", 35));
        persons.add(new Person("Alice", 40));
        persons.add(new Person("Bob", 25));

        // 根据 name 字段进行过滤和去重
        List<Person> filteredAndDistinctPersons = persons.stream()
                .filter(person -> person.getName().equals("Alice"))
                .distinct()
                .collect(Collectors.toList());

        // 打印结果
        filteredAndDistinctPersons.forEach(System.out::println);
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        returnPerson [name=" + name + ", age=" + age + "]";
    }
}

In this example, we created a Person class to represent personnel information, including two fields of name and age. We use the filter method of the stream to filter out objects whose name field is "Alice", and then use the distinct method to remove duplicate objects. Finally, we collect the results into a new collection using the collect method.

Running the sample code produces the following output:

Person [name=Alice, age=25]
Person [name=Alice, age=40]

These two results are the results after filtering and removing duplicates based on the name field being "Alice".

There are now two collection lists. How to deduplicate a list according to an attribute field in another list to produce a result that does not contain it?

To deduplicate elements in one collection that do not contain the attribute field based on an attribute field in another collection, you can use the Stream and filter operations in Java 8+. Here is a sample code showing how to do it:

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

public class Main {
    public static void main(String[] args) {
        // 创建两个对象集合
        List<Person> list1 = new ArrayList<>();
        list1.add(new Person("Alice", 25));
        list1.add(new Person("Bob", 30));
        list1.add(new Person("Charlie", 35));

        List<Person> list2 = new ArrayList<>();
        list2.add(new Person("Alice", 25));
        list2.add(new Person("David", 40));
        list2.add(new Person("Eve", 45));

        // 根据 list2 中的 name 字段,将 list1 中不包含该字段的元素去重
        List<Person> distinctPersons = list1.stream()
                .filter(person -> list2.stream().noneMatch(p -> p.getName().equals(person.getName())))
                .distinct()
                .collect(Collectors.toList());

        // 打印结果
        distinctPersons.forEach(System.out::println);
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        returnPerson [name=" + name + ", age=" + age + "]";
    }
}

In this example, we create two collections list1 and list2, representing two collections of objects respectively. We use the filter method of the stream to filter out elements in list1 that do not contain the name field in list2, and then use the distinct method to remove duplicates. Finally, we collect the results into a new collection using the collect method.

Running the sample code produces the following output:

Person [name=Bob, age=30]
Person [name=Charlie, age=35]

These two results are based on the name field in list2, and the elements in list1 that do not contain this field are deduplicated.

Guess you like

Origin blog.csdn.net/weixin_50503886/article/details/132310147