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

时间:2019-01-21 21:30:52

标签: filter java-8 set java-stream flatmap

我上课

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"))
        );
    }

}

问题是,如何使用流和flatMap打印年龄在21岁以上的人及其兴趣而不重复。因此,总的来说,我应该在曲棍球,足球和谷歌上获得“亚历克斯”,在排球和网球上获得“费利克斯”(曲棍球是重复的)

2 个答案:

答案 0 :(得分:1)

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]}]

答案 1 :(得分:1)

这也将起作用。

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()。