Functional interfaces, lambda syntax, method references, Optional, and the Stream API with map, filter, reduce and collectors.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a functional interface in Java?
Show answer
An interface with exactly one abstract method, used as the target type for lambda expressions and method references.
- What does the @FunctionalInterface annotation do?
Show answer
It marks an interface as functional and causes a compile error if the interface violates the single abstract method rule.
- What is the Single Abstract Method (SAM) rule?
Show answer
A functional interface must have exactly one abstract method; default and static methods do not count.
- Can a functional interface have default or static methods?
Show answer
Yes, it can have any number of default and static methods; only the count of abstract methods matters.
- What does the Function<T,R> functional interface represent?
Show answer
A function that takes a parameter of type T and returns a result of type R.
- What is the basic syntax of a lambda expression?
Show answer
(parameters) -> body, where parameters are the inputs and body is the expression or statement block.
- How do you write a lambda with zero parameters?
Show answer
() -> expression or () -> { statements; }, using empty parentheses.
- How do you write a lambda with a single parameter without explicit type?
Show answer
x -> expression, omitting the parentheses when there is exactly one parameter and no type is declared.
- How do you write a lambda with multiple parameters?
Show answer
(x, y) -> expression or (x, y) -> { statements; }, with parentheses around multiple parameters.
- What is type inference in lambda expressions?
Show answer
The compiler deduces parameter types from the context, so you can omit explicit types like (String x) -> x.length().
- How do you write a multi-statement lambda?
Show answer
(params) -> { statement1; statement2; return result; }, using curly braces and explicit return statement.
- What is variable capture in lambdas?
Show answer
A lambda can access and use variables from its enclosing scope (captured variables).
- Why must captured local variables be final or effectively final?
Show answer
To ensure thread safety and prevent unexpected behavior; if a variable could change, the lambda's behavior would be unpredictable.
- What are method references?
Show answer
A shorthand for lambdas that call a single existing method, using the syntax ClassName::methodName.
- What is the syntax for a static method reference?
Show answer
ClassName::staticMethodName, for example String::valueOf or Math::abs.