CH8 - Lambdas and FI Flashcards

1
Q

Lambda expression

A

LE - is a block of code that gets passed around. You can think of a LE as an unnamed method existing inside a anonymous class.

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

Lambdas

A

✅ Lambdas can be used to implement functional interfaces (interfaces with one abstract method).
✅ Lambdas can call default methods but cannot override them.
❌ Lambdas cannot override or redefine default methods; you must use a concrete or anonymous class instead.

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

Anonymous class

A

✅ Anonymous classes are used for quick, one-time implementations.
✅ They implement interfaces or extend classes (abstract or concrete).
✅ They can override default methods of an interface.
✅ They cannot have a constructor (since they don’t have a name).

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

Functional Interface

A

SAM - Single Abstract Method

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

Functional Interface Rules

A

✔ Exactly one abstract method (SAM - Single Abstract Method)
✔ Can have default and static methods
✔ Can extend another functional interface (without adding new abstract methods)
✔ @FunctionalInterface annotation is optional but recommended
✔ Methods from Object class do not count as abstract methods
✔ Supports lambda expressions and method references

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

Adding Object Methods

A

If a FI includes an abstract method with the same signature as a public method found in Object, those methods do not count toward the single abstract method test.

It is valid FI.

public interface Dive {
//
String toString();
public boolean equals(Object o);
public abstract int hashCode();
public void dive();
}

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

:: on Lambdas

A

:: is like a lambda, and it is used for deferred execution with a functional interface. You can even imagine the method reference a s a lambda if it helps you.

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

Formats of method reference

A
  • static methods
  • Instance methods on a particular object
  • Instance methods on a parameter to be determined at runtime
    -Constructors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Consumer FI

A

You use a Consumer when you want to do something with a parameter but not return anyting.

@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}</T>

Consumer<String> c1 = System.out::println;</String>

c1.accept(“Annie”); //Annie

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

Supplier FI

A

Supplier is used when you want to generate or supply values without taking any input.

@FunctionalInterface
public interface Supplier<T> {
T get();
}</T>

Supplier<LocalDate> s1 = LocalDate::new;
s1.get() //2025-02-20</LocalDate>

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

BiConsumer FI

A

Does same thing as Consumer except that it takes two parameters.

@FunctionalInterface
public interface BiConsumer<T, U> {
void accept(T t, U u);
}

var map = new HashMap<String, Integer>();
BiConsumer<String, Integer> b1 = map::put;

b1.accept(“Chicken”, 100);

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

Predicate FI

A

Predicate is oftern used when filtering or matching.

@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}</T>

Predicate<String> s1 = String::isEmpty;
sout(p1.test(""); //true</String>

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

BiPredicate FI

A

Same as Predicate but accepts two parameters

@FunctionalInterface
public interface BiPredicate<T, U> {
boolean test(T t, U u);
}

BiPredicate<String, String> b1 = String::startsWith;
sout(b1.test(“chicken”, “chick”); //true

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

Function FI

A

A Function is responsible for turning one parameter into a value of potentially different type and returning it.

@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}

Function<String, Integer> f1 = String::length;
sout(f1.apply(“cluck”)); //5

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

BiFunction FI

A

BiFunction is responsible for turning two parameters into a value and returning it.

@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}

BiFunction<String, String, String> b1 = String::concat;
sout (b1.apply(“baby “, “chick”)); //baby chick

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

UnaryOperator and BinaryOperator FI

A

They are special cases of Function.
They require all type parameters to be the same type.

UnaryOperator transforms its value into one of the same type.

BinaryOperator merges two values into one of the same type.

@FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> {
//
}</T>

@FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T, T, T> {
//
}</T>

17
Q

Variables in Lambdas

A

They appear in three places:
-parameter list
-Local variables declared inside the lambda body
-variables referenced from lambda body

18
Q

Parameter List formats in Lambda

A

-Without types
-With types
-With var

Compiler requires all parameters in the lambda to use the same format

19
Q

Local variables inside a lambda body

A

It is legal to define a block inside lambda body.
That block can have anything that is valid in a normal Java block including local variable declarations.

20
Q

Referencing Variables from Lambda body

A

Lambda bodies are allowed to reference some variables from the surrounding code.

Lambda can access an instance variable, method parameter, or local variable under certain conditions.

Instance variables (and class variables) are always allowed.

The only thing lambdas cannot access are variables that are not final or effectively final.