lambda 之Stream流

5、collector——将stream流中的元素收集起来

 R collect(Collector collector); Collector collector 是一个接口,需用现实类来操作。jdk8提供了Collector接口的工具类Collectors用于将元素收集为list 、set 、map等

实例:

Book book = new Book("JAVA","ALIS", Book.Status.Busy,1009);
Book book2 = new Book("JAVASE","MARRY",Book.Status.Busy,393.234);
Book book3 = new Book("JAVAEE","LUCY",Book.Status.Free,3434.112);
Book book4 = new Book("MYBATIS","DANDAN",Book.Status.Busy,3423.12);

//收集名字放到list中

ArrayList books = new ArrayList<>();
books.add(book);
books.add(book2);
books.add(book3);
books.add(book4);

List collect = books.stream()
        .map(Book::getName)
        .collect(Collectors.toList());
System.out.println(collect);

你可能感兴趣的:(lambda,lambda)