Java 8 Streams Flashcards
Till Java 7 it provided abstraction such as inheritance and generics, what did these things abstract?
Abstraction over data
Lambda expressions provide abstraction over what?
Abstraction over behavior
Does lambda and streams make Java functional language?
No, functions are still not first class citizens in Java.
What was main goal behind Java 8 lambda and streams?
The main goal was to make it easy, less error-prone to write parallel and concurrent code and utilize multiple cores available. At the same time improve readability of such code.
By default till Java 7, for each loop was serial or parallel?
It was fixed as serial implementation. We couldn’t use multiple cores for iteration even if available. We had to write parallel code ourselves which is very hard to get right.
Example where serial model is bottleneck
Suppose someone asked you to mail some letters with following instructions: If you have any more mails, take next one in alphabetical order of addressee’s surname and put it in mailbox. Obviously problem is overspecified. Ordering doesn’t matter in this task, neither does the mode serial or parallel.
coll.forEach(Consumer), then Consumer is example of which GoF design pattern?
Command design pattern. Caller just knows what operation he needs to perform and iteration is done by foreach()
Why were lambda expressions added?
- To make it less clumsy to add implementation of FunctionalInterfaces. Before that only way to do that was either create a dedicated class or create anonymous inner class.
- It reduced the verbosity, as rest of the things like method name of functional interfaces, interface name could be inferred by compiler.
Stream allows us to view program in what way?
Data oriented way of looking at the program.
What are stream and operators analogous to?
Unix pipes and filters
What do streams represent?
Streams represent data in motion and don’t provide any way to store the data.
Name Streams for primitives?
IntStream, LongStream, DoubleStream etc.
Is stream eager or lazy?
Streams are lazy, all the intermidiate operators are lazy and the terminal operators like max, min, forEach() etc start pulling data from the source lazily.
What can be sources of streams?
Collections, Arrays or Generating functions like IntStream.generate(..)
What is recursive decomposition?
The process of splitting tasks into smaller ones to be executed in parallel, till they are “small enough” to be executed in serial. The technique for splitting depends on source of data.