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()});
}
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With