Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning specific element from list

Tags:

java

Hi i want to retrieve only the name of students from list, i used filter method but it returns boolean,so is there any other method to do so?

public class Main {

    public static void main(String[] args) {
        Collection<Student> students=new LinkedList<>();
        students.add(new Student("Add","Nitkons",01));
        students.add(new Student("Nina","Adinson",02));
        students.add(new Student("Mick","McDonald",05));
        students.add(new Student("Anna","Lavrova",04));


        //doesnt work
        Stream<Student> x=students.stream().filter(s->{return s.getName()});

    }
}
like image 331
Michel Berger Zvain Avatar asked Jan 09 '23 03:01

Michel Berger Zvain


1 Answers

You need map :

Stream<String> names = students.stream().map(Student::getName);

And to collect the names to a List :

List<String> names = students.stream().map(Student::getName).collect(Collectors.toList());
like image 65
Eran Avatar answered Jan 19 '23 08:01

Eran