从列表中返回特定元素

时间:2015-06-17 11:59:12

标签: java

您好我只想从列表中检索学生的名字,我使用过滤方法,但它返回boolean,那么有没有其他方法可以这样做?

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

    }
}

1 个答案:

答案 0 :(得分:9)

你需要地图:

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

并将名称收集到列表中:

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