Java

Java 8 Stream API: 8 Interview Questions You Should Be Able to Answer

7 min read R Square IT Support TeamSample article

Streams appear in almost every Java interview. Here are common Stream API questions with clear explanations and code you can practise.

The Stream API, introduced in Java 8, lets you process collections in a declarative way. Interviewers use it to check whether you write modern Java and whether you understand what happens behind the method chain.

1. What is the difference between intermediate and terminal operations?

Intermediate operations such as map, filter and sorted return a new stream and are lazy — nothing runs until a terminal operation is called. Terminal operations such as collect, forEach, count and reduce trigger processing and produce a result or side effect.

2. Find the frequency of each character in a string

java
Map<Character, Long> freq = "programming".chars()
    .mapToObj(c -> (char) c)
    .collect(Collectors.groupingBy(
        Function.identity(),
        LinkedHashMap::new,
        Collectors.counting()));
// {p=1, r=2, o=1, g=2, a=1, m=2, i=1, n=1}

3. Find the second highest number in a list

java
Optional<Integer> second = numbers.stream()
    .distinct()
    .sorted(Comparator.reverseOrder())
    .skip(1)
    .findFirst();

4. map vs flatMap

map transforms each element into exactly one element. flatMap transforms each element into a stream and then flattens all those streams into one — useful for a List<List<String>> or for splitting sentences into words.

5. Group employees by department

java
Map<String, List<Employee>> byDept = employees.stream()
    .collect(Collectors.groupingBy(Employee::getDepartment));

Map<String, Double> avgSalary = employees.stream()
    .collect(Collectors.groupingBy(
        Employee::getDepartment,
        Collectors.averagingDouble(Employee::getSalary)));

6. findFirst vs findAny

findFirst returns the first element in encounter order. findAny may return any element and can be faster in parallel streams because it does not need to respect order.

7. Can a stream be reused?

No. Once a terminal operation has run, the stream is consumed. Calling another operation throws IllegalStateException. Create a new stream from the source, or use a Supplier<Stream<T>> if you need to build it repeatedly.

8. When should you avoid parallel streams?

  • Small collections, where thread overhead outweighs any gain
  • Operations with shared mutable state or side effects
  • Order-sensitive processing
  • Blocking I/O inside the pipeline

Practise writing each example without an IDE, then explain it aloud. Being able to describe what each step does is what turns a correct answer into a strong one.

  • Java 8
  • Streams
  • Interview

This is a sample article published as a starter resource. Verify technical details against official documentation for the versions you use.

Prepare. Perform. Progress.

Want personal guidance on this topic?

Our team can help you prepare, solve doubts and practise interviews around what you are learning.

Chat with us