Java_8_Interview_Problems
Java_8_Interview_Problems
What are functional interfaces and how are they used in Java 8?
Functional interfaces have a single abstract method and can be implemented using
lambdas.
Examples:
Runnable, Callable, Comparator, Function, Consumer, Supplier, Predicate.
You can annotate them with @FunctionalInterface (optional but recommended).
Custom example:
interface Converter<F, T> { T convert(F from); }
Used heavily in stream operations, callbacks, and APIs like CompletableFuture.
How does the Stream API work and what are the benefits of using it?
Streams provide a high-level abstraction for processing sequences of elements in a
declarative style.
Key operations:
Intermediate: filter, map, sorted, distinct.
Terminal: collect, forEach, reduce, count, anyMatch.
Streams can be sequential or parallel.
Advantages:
Encourages functional composition.
Reduces boilerplate loops.
Enables lazy evaluation and potential parallelism.
What are default and static methods in interfaces and how do they impact
design?
Java 8 allows interfaces to have default and static methods.
default:
Provides a method implementation within the interface.
Used to evolve interfaces without breaking existing implementations.
static:
Utility methods that belong to the interface itself, not instances.
Design impact:
Breaks strict separation of interface and implementation.
Should be used carefully to avoid bloating interfaces.