Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

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

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

For example, I have a Person class:

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 Stream API, like this:

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 List of a property with Stream API?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use 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 :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...