如何使用stream和flatMap过滤数据?

How to filter data using stream and flatMap?

我有一个class

public class Person
{
    private int          _id;
    private String       _name;
    private int          _age;
    private List<String> _interests;

    public Person()
    {
    }

    public Person(int id, String name, int age, List<String> interests)
    {
        _id = id;
        _name = name;
        _age = age;
        _interests = interests;
    }

    public int getId()
    {
        return _id;
    }

    public void setId(int id)
    {
        _id = id;
    }

    public String getName()
    {
        return _name;
    }

    public void setName(String name)
    {
        _name = name;
    }

    public int getAge()
    {
        return _age;
    }

    public void setAge(int age)
    {
        _age = age;
    }

    public List<String> getInterests()
    {
        return _interests;
    }

    public void setInterests(List<String> interests)
    {
        _interests = interests;
    }
}

我有主要方法

public class Main
{
    public static void main(String[] args)
    {
        List<Person> people = Arrays.asList(
            new Person(1, "Alex", 23, Arrays.asList("hockey", "football", "google")),
            new Person(2, "Brad", 18, Arrays.asList("hockey", "tennis")),
            new Person(3, "Felix", 22, Arrays.asList("volleyball", "tennis", "hockey")),
            new Person(4, "Brandon", 19, Arrays.asList("hockey", "football", "google"))
        );
    }

}

问题是,如何使用 stream 和 flatMap 打印年龄超过 21 岁的人及其兴趣而不重复。所以,一般来说,我应该 "Alex" 曲棍球,足球和 google 和 "Felix" 排球和网球(曲棍球是重复的)

interestSet维护已经处理过的人的兴趣,将用于过滤其他人

    List<Person> personList = new ArrayList<>();
    Set<String> interestSet = new HashSet<>();
    people.stream()
            .filter(p -> p.getAge() > 21)
            .forEachOrdered(p -> {
                List<String> interest = p.getInterests().stream()
                        .filter(i -> !interestSet.contains(i))
                        .collect(Collectors.toList());
                interestSet.addAll(interest);
                p.setInterests(interest);
                personList.add(p);

            });
    System.out.println(personList);

输出

[Person{_id=1, _name='Alex', _age=23, _interests=[hockey, football, google]}, Person{_id=3, _name='Felix', _age=22, _interests=[volleyball, tennis]}]

这也行。

import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    public static void main(String[] args)
    {
        List<Person> people = Arrays.asList(
            new Person(1, "Alex", 23, Arrays.asList("hockey", "football", "google")),
            new Person(2, "Brad", 18, Arrays.asList("hockey", "tennis")),
            new Person(3, "Felix", 22, Arrays.asList("volleyball", "tennis", "hockey")),
            new Person(4, "Brandon", 19, Arrays.asList("hockey", "football", "google"))
        );

        Set<String> interestSet = new HashSet<>();

        people.stream().filter(f -> f.getAge() >21).flatMap(f -> {
            List<String> list = f.getInterests().stream().filter(in -> !interestSet.contains(in)).collect(Collectors.toList());
            for (String i : list) {
                interestSet.add(i);
            }
            return Arrays.asList(new Person(f.getId(),f.getName(),f.getAge(),list)).stream();
        }).forEach(p -> System.out.println(p.getName() + " " + p.getAge() + " "+ p.getInterests()));
    }

您也可以尝试将 if(f.getAge() >21) 放在 flatMap 中以删除 filter()。