Basic Stream API Logical Interview Questions: The Java Stream API is one of the most common topics in Java interviews, especially for developers with 3–10 years of experience. Interviewers often test your ability to solve logical problems with collections using streams instead of traditional loops.
In this blog, we’ll solve the Top 10 basic Java Stream API interview questions on integer collections, each with a separate Java program.
Top 10 basic Java Stream API interview questions
1. Given a list of integers, find the maximum and minimum values
Finding max and min is one of the most basic but frequently asked stream problems.
import java.util.*;
import java.util.stream.*;
public class MaxMinExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 12, 8, 3, 21, 7);
int max = numbers.stream().max(Integer::compare).get();
int min = numbers.stream().min(Integer::compare).get();
System.out.println("Max: " + max);
System.out.println("Min: " + min);
}
}
OUTPUT:
Max: 21
Min: 3
2. Find the second-highest and second-lowest numbers in a list
This tests your ability to work with sorting and skipping elements in streams.
import java.util.*;
import java.util.stream.*;
public class SecondHighLowExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 5, 8, 30, 15);
int secondHighest = numbers.stream()
.sorted(Comparator.reverseOrder())
.skip(1)
.findFirst()
.get();
int secondLowest = numbers.stream()
.sorted()
.skip(1)
.findFirst()
.get();
System.out.println("Second Highest: " + secondHighest);
System.out.println("Second Lowest: " + secondLowest);
}
}
OUTPUT:
Second Highest: 20
Second Lowest: 8
3. From a list, remove all duplicate numbers
A common interview question to check knowledge of distinct()
.
import java.util.*;
import java.util.stream.*;
public class RemoveDuplicatesExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 10, 30, 20, 40);
List<Integer> uniqueNumbers = numbers.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("Unique Numbers: " + uniqueNumbers);
}
}
OUTPUT:
Unique Numbers: [10, 20, 30, 40]
4. Count how many times each number occurs in a list
Frequently asked because it uses grouping and counting collectors.
import java.util.*;
import java.util.stream.*;
public class FrequencyCountExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 10, 30, 20, 40, 10);
Map<Integer, Long> frequency = numbers.stream()
.collect(Collectors.groupingBy(n -> n, Collectors.counting()));
System.out.println("Frequency Count: " + frequency);
}
}
OUTPUT:
Frequency Count: {20=2, 40=1, 10=3, 30=1}
5. Calculate the sum of all numbers in a collection
Simple but common — demonstrates reduce()
and mapToInt()
.
import java.util.*;
import java.util.stream.*;
public class SumExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 10, 15, 20);
int sum = numbers.stream()
.mapToInt(Integer::intValue)
.sum();
System.out.println("Sum: " + sum);
}
}
OUTPUT:
Sum: 50
6. Find the average value from a list of numbers
Tests knowledge of mapToInt().average()
.
import java.util.*;
import java.util.stream.*;
public class AverageExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 10, 15, 20);
double avg = numbers.stream()
.mapToInt(Integer::intValue)
.average()
.getAsDouble();
System.out.println("Average: " + avg);
}
}
OUTPUT:
Average: 12.5
7. Extract all numbers greater than 10 from a list
Basic use of filter()
in interviews.
import java.util.*;
import java.util.stream.*;
public class FilterGreaterExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 12, 18, 7, 25, 3);
List<Integer> result = numbers.stream()
.filter(n -> n > 10)
.collect(Collectors.toList());
System.out.println("Numbers greater than 10: " + result);
}
}
OUTPUT:
Numbers greater than 10: [12, 18, 25]
8. Sort a list of integers in both ascending and descending order
Sorting is always tested in interviews with streams.
import java.util.*;
import java.util.stream.*;
public class SortExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(15, 3, 25, 8, 12);
List<Integer> ascending = numbers.stream()
.sorted()
.collect(Collectors.toList());
List<Integer> descending = numbers.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println("Ascending: " + ascending);
System.out.println("Descending: " + descending);
}
}
OUTPUT:
Ascending: [3, 8, 12, 15, 25]
Descending: [25, 15, 12, 8, 3]
9. Merge two lists of integers into one, ensuring there are no duplicates
A real-world style interview question testing Stream.concat()
.
import java.util.*;
import java.util.stream.*;
public class MergeListsExample {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> list2 = Arrays.asList(4, 5, 6, 7, 8);
List<Integer> merged = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
System.out.println("Merged List: " + merged);
}
}
OUTPUT:
Merged List: [1, 2, 3, 4, 5, 6, 7, 8]
10. From a list of integers, separate even and odd numbers into two different results
Often asked to test knowledge of partitioningBy()
.
import java.util.*;
import java.util.stream.*;
public class PartitionEvenOddExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Map<Boolean, List<Integer>> partition = numbers.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
System.out.println("Even Numbers: " + partition.get(true));
System.out.println("Odd Numbers: " + partition.get(false));
}
}
OUTPUT:
Even Numbers: [2, 4, 6, 8]
Odd Numbers: [1, 3, 5, 7, 9]
Conclusion
These 10 Java Stream API interview questions cover the fundamentals of integer collections — from finding max/min and removing duplicates to filtering, sorting, and partitioning. By practicing these problems, you’ll build confidence in writing clean, functional Java code with the Stream API.
In the next post of this series, we’ll dive into Intermediate Stream API Problems often asked in interviews.