Below are important Question & Answers taken from the Q&A discussions in this section.
Q1) How is lambda implemented internally?
How exactly it is implemented is dependent on JVM. Compiler only inserts a byte code instruction called invokedynamic (representing the lambda) and how the actual implementation would be is a JVM thing. Below is a good post on this. Essentially, what they are saying is at runtime, when JVM encounters the invokedynamic instruction for the first time, it creates a class that implements the functional interface and also creates a synthetic method for lambda. An instance of this class is what is returned. Generally, this object is re-used for subsequent executions of that instruction (except in certain cases where it is recreated every time).
https://stackoverflow.com/questions/26257266/are-java-8-lambdas-compiled-as-inner-classes-methods-or-something-else
Q2) In a stream pipeline, once a terminal operation is triggered, in what order are the operations executed?
The evaluation goes in the reverse order from terminal to intermediate in order. We had the following code in the lecture on Slicing. So, the order would be forEach -> map -> limit -> distinct -> filter -> a book item. If the book item fulfills the rating condition, then it would be passed to the downstream operation (distinct), which relays to the next one and so on until forEach is reached. But, if the book item did not fulfill the rating condition, then next item is pulled from stream. Take a look at the below post too, which gives a similar example and explains very nicely. You can take a look at the second answer from user Umberto Raimondi.
https://stackoverflow.com/questions/29915591/java-8-stream-operations-execution-order
books.stream()
.filter(d -> d.getRating() >= 4.5)
.distinct()
.limit(5)
.map(d -> d.getTitle())
.forEach(System.out::println);