Chapter 6 Flashcards

1
Q

What is a lambda expression?

A

A lambda expression is a short block of code which takes in parameters and returns a value.

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

What is an interface with one method called?

A

A functional interface

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

Name the four most used functional interfaces and their packages

A
  • Predicate
  • Consumer
  • Supplier
  • Comparator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When do you want to use the Predicate functional interface?

A

To test a method that returns a boolean value.

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

When do you want to use the Consumer functional interface?

A

When you want to do something without returning a value (such as printing something)

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

When do you want to use the Supplier functional interface?

A

The supplier takes zero parameters and thus is often used for generating values.

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

Is this code valid?

(a, b) -> { int a = 0; return 5; }

A

You cannot redeclare a, as a is already defined in the scope (parameter a).

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

Under what conditions are method parameters and local variables allowed to be referenced by a lambda expression?

A

If they are effectively final. (page 235)

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

What are the rules for accessing a variable from a lambda body inside a method?

A
  • Instance variable: allowed
  • Static variable: allowed
  • Local variable: allowed if effectively final
  • Method parameter: allowed if effictively final
  • Lambda parameter: allowed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List and Set declare a removeIf() method. What functional interface does it take as a parameter?

A

A Predicate.

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

What functional interface does the method forEach() take as a parameter?

A

A Consumer.

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