Lambda Flashcards

1
Q

What is actually lambda?

A

It is quick implementation of an functional interface, a shortcut for compiler.
Compiler is implementing a anonymous class instead of lambda later.

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

What is functional interface?

A

It is any interface that has only one abstract method.
It can be marked with @FunctionalInterface, but it is not mandatory.

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

Is empty lambda valid?

A

Yes.
Eg. s -> {}

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

Is “var” allowed as type reference for lambda?

A

No, there is no context in using “var”. It can be used inside lambda, or as parameter type.

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

What methods are ignored when defined in functional interface?

A

equals()
hashCode()
toString()

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

Can modifiers be used with lambda parameters?

A

Yes.
Eg. (final var x, @Deprecated var y) ->x.compareTo(…

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

What are method references?

A

Quick implementation of functional interface and what will be ran in implementation of the interface method.

interface Test {
int test();
}
() -> Math.random()
Math::random
Test test = Math::random;
test.test();

What abstract method inside interface returns, so must method reference.

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

What are some of predefined utility functional interfaces?

A

Supplier
Consumer
BiConsumer
Predicate
BiPredicate
Function
BiFunction
Unary Operator
Binary Operator

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

Supplier?

A
  • no parameters
  • returns any type object
  • get() method

Supplier< Integer > supplier = () -> 1;

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

Consumer?

A
  • has any type parameter, generic
  • no returns
  • accept() method
  • can be chained with andThen()

Consumer< Integer > consumer = System.out::println;

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

BiConsumer?

A
  • has two any type parameters, generics
  • no returns
  • accept() method
  • can be chained with andThen()

BiConsumer<Integer, String> biConsumer = (a, b) -> { System.out.println(a); };

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

Predicate?

A
  • has any type parameter, generic
  • has boolean return
  • test() method
  • can be chained with and(), negate(), or()

Predicate< Integer > predicate = x -> x > 5;

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

BiPredicate?

A
  • has two any type parameters, generics
  • has boolean return
  • test() method
  • can be chained with and(), negate(), or()

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a < b

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

Function?

A
  • has any type parameter generic (first generic, first parameter)
  • has return of the generic defined type
  • apply() method
  • can be chained with compose()

Function<Integer, Integer> function = x -> x * 2

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

BiFunction?

A
  • has two any type parameter generic
  • has return of the generic defined type
  • apply() method
  • can be chained with compose()

BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;

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

UnaryOperator?

A
  • has one any type parameter generic
  • has return of the same type as parameter generic
  • apply() method
  • can be chained with compose()

UnaryOperator<Integer> unaryOperator = x -> x * 2;</Integer>

17
Q

BinaryOperator?

A
  • has two any type parameter generics
  • has return of the same type as parameter generic
  • apply() method
  • can be chained with compose()

BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;</Integer>

18
Q

What are other combining predefined functional interfaces?

A

Each combination of main functional interfaces with Double, Int, Long, and combination of converting each of them into another, eg. DoubleToIntSupplier, IntToLongPredicate etc.