Java 8 Streams Flashcards Preview

Software Developer Interview Prep > Java 8 Streams > Flashcards

Flashcards in Java 8 Streams Deck (32)
Loading flashcards...
1

Till Java 7 it provided abstraction such as inheritance and generics, what did these things abstract?

Abstraction over data

2

Lambda expressions provide abstraction over what?

Abstraction over behavior

3

Does lambda and streams make Java functional language?

No, functions are still not first class citizens in Java.

4

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.

5

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.

6

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.

7

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()

8

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.

9

Stream allows us to view program in what way?

Data oriented way of looking at the program.

10

What are stream and operators analogous to?

Unix pipes and filters

11

What do streams represent?

Streams represent data in motion and don't provide any way to store the data.

12

Name Streams for primitives?

IntStream, LongStream, DoubleStream etc.

13

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.

14

What can be sources of streams?

Collections, Arrays or Generating functions like IntStream.generate(..)

15

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.

16

What fundamental principle does Fork Join pool in Java depend on?

Recursive decomposition.

17

What is the way to telling java to use parallel execution to process the stream?

Source.parallelStream() is the way to tell java that parallel stream can be used which will utilize Fork/Join framework.

18

Use of sorted operator and comparing, by example

intList.stream()
.map(i -> new Point(i % 3, i / 3))
.sorted(comparing(point -> p.distance(0, 0)))
.forEach(print);

19

Which ordering does Comparator.comparing() return?

Natural ordering

20

Comparator.comparing() method signature?

public static > Comparator comparing(Function keyExtracter)

21

Is it allowed to provide explicit type in lambda parameters?

Yes it is allowed. But if type is provided for one it must be provided for all. And it is sometimes necessary to provide type information when the compiler cannot infer it from the context.
() -> 23
(x, y) -> x + y
(int x, int y) -> x + y

22

Expression lambda vs statement lambda

Expression lambda can have only a single line and there is an explicit return, including for void. So for example x -> x * 3, i -> print(i), (x, y) -> x + y are expression lambdas.
Statement lambda are general where you can have curly braces block where you can write multiple lines of code. But that code needs to have explicit return statement.
For example (Thread t) -> {t.start();}, () -> {System.gc(); return 0; }

An expression lambda can be seen as args -> expr
A statement lambda can be seen as args -> {return expr;}

23

Are lambda expressions allowed to declare exceptions they can throw using throws clause?

No, lambda expressions are neither required nor allowed to use throws clause to declare the exceptions they can throw.

24

Lambda vs anonymous inner class

1 - An inner class creation expression is guaranteed to create a new object with unique identity but that is not true for lambda expression. It is implementation dependent.
2 - An inner class declaration creates new naming scope where this and super refer to current inner class. By contrast lambda expressions do not introduce any naming environment. So using this in lambda refers to enclosing class in which lambda is defined.

25

Can lambda expressions have state?

No. Lambda expressions are not allowed to have state. So their identity does not matter. This allows platform flexibility to optimize in ways which would not be possible if all lambda expressions were required to have unique identity.

26

Can a lambda refer to itself?

We cannot use this to refer to lambda, but a lambda can refer to itself if its name is in the scope but there are few restrictions. It is still possible to declare recursive lambda

class Factorial {
IntUnaryOperator fact;
public Factorial() {
fact = i -> i == 0 ? 1 : i * fact.applyAsInt(i - 1);
}
}

27

Can lambda parameters and lambda body local declarations shadow field names?

Yes a field name can be temporarily redeclared as a parameter or local variable name

class Foo {
Object i, j;
IntUnaryOperator iuo = i -> { int j = 3; return i + j;}
}

28

Is it possible to redeclare a local variable in lambda?

No, it is illegal to redeclare a local variable within a lambda

void foo() {
final int i = 2;
Runnable r = () -> {int i = 3; }} // illegal
}

29

What is a non-capturing lambda?

A lambda which interact with environment only via arguments and return values are stateless or non-capturing.

30

What is a capturing lambda?

Capture is a technical term for the retention by a lambda of a reference to its environment. The connotation is that the variable has been ensnared by the lambda and held, to be queried or in other lamguages - modified, when the lambda is later evaluated.