Java 8 Flashcards

1
Q

Interface rules since Java 8 :

A

*Interface methods can have either “static” or “default” modifier
*Interface methods can have a body if they are “static” or “default” (not abstract)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why default interface methods are added on java ?

A

If we add a new method in interface , we will be forced to implement it on all implementations, which is not the case for default interface methods which already define their methods body.
+ to support lambda expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Some default interface methods rules :

A

We can override a default interface method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Diamond Problem ?

A

Java doesn’t allow multiple inheritance for classes because compiler will be confused to chose which superclass method to use.
This happens also for interfaces , when a class implement multiple interfaces with the same default method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to resolve Diamond Problem for interface ?

A

We must provide implementation for common default methods of interfaces , where class chose which one to call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is static interface method ?

A

static interface method is similar to default method except that we can’t override it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Functional Interface ?

A

*Functional interface is an interface where only a single abstract method is defined.
______________________________________________
=>Functional interface is implemented by Lambda expression.
=>Functional interface can have static and default methods
=>All methods must be public
=>Methods defined in class Object are not taken into account as being abstract methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The use of @FunctionalInterface is optional ?

A

Yes , it is optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the two advantages of @FunctionalInterface annotation ?

A

*indicates to compiler that it’s a functional interface , then compiler verify if all rules all respected
*javadoc will use annotation when generating documentation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What the package java.util.function provides ?

A

It provides standard functional interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 4 standard functional interfaces provided by java 8 ? what is there definitions ?

A

*Consumer : defines a function that take an argument but returns no value
*Supplier : defines a function that return a value as the same type of argument
*Predicate : defines a function that expect different types of arguments and return a Boolean value.
*Function : defines a function that expect different types of arguments and return a value.
_______________________________________________
Other standard Functional interfaces :
*UnaryOperator : defines an operation that expect only one parameter and return a value
*BinaryOperator : defines an operation that expect two parameters and return a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Lambda expression ?

A

Lambda expression is an anonymous method, that doesn t belong to any class and execucted on demand. It has no return type.
=> It is used to implement Functional interface and simplify functional method.
=>For parameters type,return type , exceptions : compiler deduce it from Functional Interface.
=> Lambda Expression can implement only one method from interface.
=> It is treated as a function , thus , there is no need to create .class file by compiler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which type of variables Lambda expression can capture ?

A

Local + instance + static variables
________________________________________
For local variable : they must be final of effictively final ( not declared final but don t change value ).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Method reference

A

Method reference is a simplified form of Lambda expression (both are similar). It allows as to invoke a pre-existant method : static method , non-static method or a constructor (instead of providing implementation like Lambda expression ).
=> It has three parts :
*A qualifier that specify the name of class or instance on which method will be invoked
*The operator ::
*An identifier that specify the name of method or new operator for constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Stream ?

A

Stream is a sequence of elements that supports operations that can performed on these elements in a functional style.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are Stream characteristics ?

A

*No Storage
*No modification on original data structure (only provide result based on our need ).
*Take data only from Collections or Arrays.
______________________________________________
*Functional approach : using functional style like map(),filter()…=> promote immutability and allow the code to be more declarative (what to do) rather than imperative (how to do it).
______________________________________________
*Lazy Evaluation : intermediate operation are lazy ,not executed until a terminal operation is invoked. Intermediate operation give a stream as a result (filter(), map(),sorted(),distinct()…)
______________________________________________
*Operations on Stream:
=>Intermediate operation ( view lazy above )
=>Terminal operation : produce a result . they trigger the pipeline of stream (forEach(),collect(),reduce(),anyMatch(),count() …).

17
Q

What are Parallel Streams ?

A

Parellel Streams use multiple CPU to process data concurrently ( not sequentially like ordinary Stream )

18
Q

Stream Workflow :

A

Source->Stream->Intermediate1->… -> Intermediate n -> Terminal -> Result

19
Q

what does reduce() operation ?

A

The reduction stream operations allow to produce a single result , by applying repeatedly combining operation to the elements on sequence.

20
Q

What are two basic reduce() operations elements ?

A

*Identity: initial/default value if the stream is empty
*Accumulator:takes two parameters:
=>A partial result of reduction operation
=>Next element on the stream
_______________________________________
*Combiner: used to combine the partial result of the reduction operation when the reduction is parallelized , or there is a mismatch between accumulator arguments and the types of accumulator implementation

21
Q

What are Collectors ?

A

Collectors are used to collect elements of stream into a desired data structure or for computing summary statistics.

22
Q

What are 3 types of collectors ?

A

*Reducing/summarazing into a single value or collection type
*Grouping ( by a property )
*Partitioning ( into 2 groups using conditions )

23
Q

give Collectors syntax

A

stream().collect(Collectors.CollectorFunction())

24
Q

give some Collector function

A

toList()
toSet()
joining()
groupingBy()
partitioningBy()
maping()
counting()
reducting()

25
Q

What is Optional ?

A

*It is a Container for a value that may or may not be present.
*It helps avoid NullPointerException

26
Q

Give some Optional methods and an example of it

A

isPresent(),orElseThrow(),filter(),map()
___________________________________
Example:
String nameValue = Optional.ofNullable(name).orElseThrow(IllegalArgumentException::new);

27
Q

give LocalDate and LocalTime examples

A

LocalDate localDate=LocalDate.now()
LocalTime localTime=LocalTime.now()