我如何打印出这个流?

library.stream()

             .map(book -> book.getAuthor())

             .filter(author -> author.getAge() >= 50)

             .map(Author::getLastName)

             .limit(10)

             .collect(Collectors.toList());

如何打印列表?我试过了


System.out.println(Collectors.toList());

但它给了我


 java.util.stream.Collectors$CollectorImpl@4ec6a292


慕斯709654
浏览 128回答 3
3回答

长风秋雁

您需要将此表达式分配给这样的列表,List<String> lastNameList = library.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(book -> book.getAuthor())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(author -> author.getAge() >= 50)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(Author::getLastName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.limit(10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());然后使用打印,System.out.println(lastNameList);或者你可以像这样直接打印,System.out.println(library.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(book -> book.getAuthor())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(author -> author.getAge() >= 50)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(Author::getLastName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.limit(10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList()));你实际上正在这样做,System.out.println(Collectors.toList());除了收集器类型的空对象外,没有什么可打印的,这就是你看到这个的原因,java.util.stream.Collectors$CollectorImpl@4ec6a292

拉丁的传说

使用foreach()方法在Listlibrary.stream()&nbsp; &nbsp; .map(book -> book.getAuthor())&nbsp; &nbsp; .filter(author -> author.getAge() >= 50)&nbsp; &nbsp; .map(Author::getLastName)&nbsp; &nbsp; .limit(10)&nbsp; &nbsp; .forEach(System.out::println);如果你想打印收集的列表,这里是一个例子List<Integer> l = new ArrayList<>();l.add(10);l.add(20);l.forEach(System.out::println);

青春有我

您可以使用Stream.peek打印50岁以上lastName的authors列表,如下所示:ageList<Book> library = List.of(new Book(new Author("overflow", 100)),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Book(new Author("stack", 80)),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Book(new Author("nullpointer", 49)));// you were ignoring the result of collectList<String> lastNames = library.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Book::getAuthor)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(author -> author.getAge() >= 50)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Author::getLastName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .peek(System.out::println) // this would print "overflow" and "stack"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());另外,请注意,如果您的主要目的只是打印名称而不是存储它们,您可以简单地使用forEach而不是collect,它们都是终端操作,只是 collect 具有基于 Stream 类型的返回类型,而 forEach 是void:-library.stream()&nbsp; &nbsp; &nbsp; &nbsp;.map(Book::getAuthor)&nbsp; &nbsp; &nbsp; &nbsp;.filter(author -> author.getAge() >= 50)&nbsp; &nbsp; &nbsp; &nbsp;.map(Author::getLastName)&nbsp; &nbsp; &nbsp; &nbsp;.limit(10)&nbsp; &nbsp; &nbsp; &nbsp;.forEach(System.out::println);以上所有,考虑到使用的对象类似于以下class Book {&nbsp; &nbsp; Author author;&nbsp; &nbsp; Book(Author author) {&nbsp; &nbsp; &nbsp; &nbsp; this.author = author;&nbsp; &nbsp; }&nbsp; &nbsp; // ... getters}class Author {&nbsp; &nbsp; String lastName;&nbsp; &nbsp; int age;&nbsp; &nbsp; Author(String lastName, int age) {&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; &nbsp; &nbsp; this.age = age;&nbsp; &nbsp; }&nbsp; &nbsp; // ... getters}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java