Top 15 Java8 Interview Questions for Experienced Developer

1. What is a functional interface in Java 8?

A functional interface has exactly one abstract method. It can have multiple default or static methods. Example: Runnable, Comparator, or custom:

@FunctionalInterface
interface MyFunc { void execute(); }


2. What is a lambda expression?

A lambda expression provides a concise way to represent an anonymous function. Example:

(a, b) -> a + b is a lambda that adds two numbers.


3. What is the Stream API?

The Stream API processes collections in a functional style. Example:

List<String> names = list.stream().filter(s -> s.startsWith("A")).collect(Collectors.toList());


4. What is the difference between intermediate and terminal operations in streams?

Intermediate operations return a stream (e.g., filter(), map()), while terminal operations produce a result (e.g., collect(), forEach()).


5. How do you remove duplicates from a list using streams?

List<String> unique = list.stream().distinct().collect(Collectors.toList());


6. What is the Optional class?

Optional is a container that may or may not contain a non-null value. Example:

Optional.ofNullable(name).ifPresent(System.out::println);


7. What is method reference in Java 8?

Method reference is a shorthand for calling a method via lambda. Example:

list.forEach(System.out::println);


8. How do you sort a list using streams?

list.stream().sorted().forEach(System.out::println);

Or with custom comparator: sorted((a, b) -> b.compareTo(a))


9. What is the difference between map() and flatMap()?

map() transforms each element; flatMap() flattens nested structures. Example:

list.stream().flatMap(Collection::stream)


10. How do you filter null values from a list?

list.stream().filter(Objects::nonNull).collect(Collectors.toList());


11. How do you group elements using streams?

Map<String, List<Person>> grouped = people.stream().collect(Collectors.groupingBy(Person::getCity));


12. What is the purpose of default methods in interfaces?

Default methods allow interfaces to have method implementations. Example:

default void log(String msg) { System.out.println(msg); }


13. How do you count elements in a stream?

long count = list.stream().filter(s -> s.length() > 3).count();


14. How do you convert a list to a map using streams?

Map<Integer, String> map = list.stream().collect(Collectors.toMap(String::length, Function.identity()));


15. How do you use reduce() in streams?

int sum = list.stream().reduce(0, Integer::sum);

It combines elements into a single result using an accumulator.