如何使用 Java 8 Stream 从某些类属性中获取列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30611870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:52:49  来源:igfitidea点击:

How can I get a List from some class properties with Java 8 Stream?

javacollectionsjava-8java-stream

提问by NCNecros

I have a List<Person>. I need to get a Listfrom a property of Person.

我有一个List<Person>. 我需要ListPerson.

For example, I have a Personclass:

例如,我有一个Person类:

class Person
{
    private String name;
    private String birthDate;
    public String getName() {
        return name;
    }
    public String getBirthDate() {
        return birthDate; 
    }
    Person(String name) {
        this.name = name;
    }
}

List<Person> personList = new ArrayList<>();
personList.add(new Person("David"));
personList.add(new Person("Joe"));
personList.add(new Person("Michel"));
personList.add(new Person("Barak"));

I want to get a list of names with the StreamAPI, like this:

我想通过StreamAPI获取名称列表,如下所示:

List<String> names = personList.stream().somecode().collect(Collectors.toList());
names.stream().forEach(System.out::println);

#David
#Joe
#Michel
#Barak

This code doesn't work:

此代码不起作用:

public class Main 
{
    public static void main(String[] args) 
    {
        List<Person> personList = new ArrayList<>();
        Person person = new Person("Иван");
        person.getFriends().addAll(Arrays.asList("Друг 1", "Друг 2", "Друг 3"));
        personList.add(person);
        person = new Person("Федор");
        person.getFriends().addAll(Arrays.asList("Друг 4", "Друг 5", "Друг 6"));
        personList.add(person);
        person = new Person("Алексей");
        person.getFriends().addAll(Arrays.asList("Друг 7", "Друг 8", "Друг 9"));
        personList.add(person);
        person = new Person("Константин");
        person.getFriends().addAll(Arrays.asList("Друг 10", "Друг 11", "Друг 12"));

        List<String> friens = personList.stream().map(e->e.getFriends()).collect(Collectors.toList());

        friends.stream().forEach(System.out::println);
        //Друг 1
        //Друг 2
        //Друг 3
        //Друг 4
        //...

    }
}

class Person
{
    String name;
    List<String> friends;

    Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public List<String> getFriends() {
        return friends;
    }
}


How can I get a Listof a property with StreamAPI?


如何List使用StreamAPI获取属性?

采纳答案by Eran

You can use map:

您可以使用map

List<String> names = 
    personList.stream()
              .map(Person::getName)
              .collect(Collectors.toList());

EDIT :

编辑 :

In order to combine the Lists of friend names, you need to use flatMap:

为了组合好友姓名列表,您需要使用flatMap

List<String> friendNames = 
    personList.stream()
              .flatMap(e->e.getFriends().stream())
              .collect(Collectors.toList());