8. Lambda Expressions and Functional Interfaces Flashcards

1
Q

What does the flatmap expects?

A

The flatmap expects a function that will take on element and create a Stream out of it

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

What does the mapToDouble method expects?

A

The mapToDouble method expects a ToDoubleFunction object. that will take an argument and return a double

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

Consumer

A

Represents an operation that accepts a single input argument and returns no result

public interface Consumer{
    void accept(T t);
}

Consumer consumer = s -> System.out.println(s);

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

Supplier

A

Represents an operation that does not take any argument and returns any kind of object aslong as it matches the type.

public interface Supplier{
    T get();
}

Supplier supplier =
() -> “Hello!;

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

Function

A

represents an operation that takes any object and may return any kind of object of another type.

public interface Function{
R apply(T t);
}

Function getName =
user -> user.getName();

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

Predicate

A

represents an operation that takes any kind of object, test it and returns a boolean

public interface Predicate{
    boolean test(T t);
}

Predicate isEmpty =
s -> s.isEmpty();

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

What does the forEach method expects?

A

The forEach() method expects a consumer as an argument

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

What does the map’s forEach method expects?

A

The map’s forEach method requires a bifurcation, a function that takes two arguments

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

Do you need parentheses by the parameters in a lambda?

A

you can leave off the parantheses around the parameter, however if you have
more than one parameter you must use the parentheses.

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

do you need curly braces when returning an expression?

A

If you try to return a single expression without curly braces, your lambda expression won’t compile. But you can use it only in curly braces.

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

Can you acces variables of the enclosing scope?

A

A lambda expression captures variables from the ecnlosing scope, so you can access those variables in the body of the lambda, but those variables must be final or effectively final. An effectively final variable is a variable or parameter whose value isn’t changed after it is initialized. You can’t modify the values of an variable in the lambda expression, otherwise you get an error.

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

What does the @FuncionalInterface do?

A

We can explicity define a functional interface using the @FunctionalInterface annotation. This
annotation is not required but can be helpful when you want to ensure you don’t inadvertently add methods to a functional interface that will then break other code.

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

What are the 4 basic types of functional interfaces?

A

Supplier, Consumer, Predicate and Function

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

What are the variations on supplier?

A

IntSupplier, DoubleSupplier, LongSupplier. The functional method names of each of
these is different and is not get(), the funtional method of IntSupplier is getAsInt().

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

What are the variations on consumer?

A

IntConsumer, DoubleConsumer, LongConsumer, their

accept() methods take one primitive argument and avoid the autoboxing you get with consumer.

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

what are variations on the primitive consumers?

A

there is ObjIntConsumer, ObjDoubleConsumer, ObjLongConsumer, whose
accept() methods take an object (type t) and a int , a double or a long.

17
Q

What is a BiConsumer?

A

BiConsumer, its accept() method takes two object (types T and U, meaning the two objects don’t
have to be of the same type).

18
Q

What do you need to remember with Lambdas?

A

you can’t modify a variable within the lambda, otherwise you will get a compiler error.
And outside the a lambda expression the variables must be final or effectively final.

19
Q

Can we modify a field of an object?

A

If we want to use forEach() to iterate through a collection of objects to, we can’t return the value we find (because forEach() takes a consumer and the accept() of a consumer method is void), but we can change the field of an object from within the lambda to do the same thing.

20
Q

What does the andThen() method do?

A

andThen() is actually a default method of the Consumer interface that you can use to chain
consumers together. Notice that the type of the consumer you pass to the
andThen() method must match the type of the consumer used as the first operation.

21
Q

What default methods does the predicate interface have?

A

The default methods:

and(), or(), and negate(), are there so you can chain predicates together

22
Q

What are the variations on predicate?

A

BiPredicate, DoublePredicate, IntPredicate, LongPredicate. BiPredicate’s test takes arguments and the others take one argument of a primitive type (to avoid
autoboxing).

23
Q

What does the UnaryOperator do?

A

The one operator you should be familiar with for the exam is the UnaryOperator. It extends Function, so its functional method is also apply(). However it requires that the type of the argument to apply() be the same as the type of the return value.

24
Q

What is method reference?

A

You can create method references for your own methods, too:

Trees.forEach(::);