Back to questions
JuniorTheoryCommon
What does the program print?
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Book> books = List.of(new Book("Dune", 1965),
new Book("Hamlet", 1603), new Book("Emma", 1815));
System.out.println(books.stream()
.filter(b -> b.year() > 1700)
.map(Book::title)
.toList());
}
}
record Book(String title, int year) {}[Dune, Emma]
- Expecting
[Hamlet]:filterkeeps the books for whichb.year() > 1700is true and drops the rest. - Expecting
Book::titlenot to compile: a record's accessor is a method named after its component,title(), so a method reference can point to it.
- How do you print the number of these books instead of their titles?
- What happens if
filterandmapswap places in this pipeline?