Advanced Java Stream API Logical Programs: Java Stream API is one of the most powerful features introduced in Java 8. While basic and intermediate problems test your foundation, interviewers often go one step further and present advanced Stream API challenges. These questions evaluate your ability to combine multiple operations and solve real-world logical problems efficiently.
In this post, we’ll cover advanced-level coding problems (21–28) on integer collections that are frequently asked in interviews.
If you missed the previous parts of this series, check them out here:
- Top 10 Basic Stream API Logical Interview Questions on Integer Collections (1–10)
- Intermediate Java Stream API Coding Problems for Interview Preparation (11–20)
Advanced Java Stream API Logical Programs
21. From a list, remove all numbers greater than 100.
import java.util.*;
import java.util.stream.Collectors;
public class RemoveGreaterThan100 {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(50, 120, 80, 200, 99, 101);
List<Integer> result = numbers.stream()
.filter(n -> n <= 100)
.collect(Collectors.toList());
System.out.println("Numbers <= 100: " + result);
}
}
OUTPUT:
Numbers <= 100: [50, 80, 99]
22. Given two lists of integers, find the numbers that are present in both.
import java.util.*;
import java.util.stream.Collectors;
public class CommonElements {
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> common = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
System.out.println("Common elements: " + common);
}
}
OUTPUT:
Common elements: [4, 5]
23. From two lists, identify numbers that appear in one list but not in the other.
import java.util.*;
import java.util.stream.Collectors;
public class UniqueFromTwoLists {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(10, 20, 30, 40);
List<Integer> list2 = Arrays.asList(30, 40, 50, 60);
List<Integer> unique =
new ArrayList<>(list1.stream().filter(n -> !list2.contains(n)).toList());
unique.addAll(list2.stream().filter(n -> !list1.contains(n)).toList());
System.out.println("Unique elements from both lists: " + unique);
}
}
OUTPUT:
Unique elements from both lists: [10, 20, 50, 60]
24. Reverse the order of elements in a list of integers.
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ReverseList {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> reversed = IntStream.rangeClosed(1, numbers.size())
.mapToObj(i -> numbers.get(numbers.size() - i))
.collect(Collectors.toList());
System.out.println("Reversed list: " + reversed);
}
}
OUTPUT:
Reversed list: [5, 4, 3, 2, 1]
25. Given a sequence of numbers from 1 to N with one missing, find the missing number.
import java.util.*;
import java.util.stream.IntStream;
public class MissingNumber {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 5, 6); // Missing 4
int n = 6;
int expectedSum = IntStream.rangeClosed(1, n).sum();
int actualSum = numbers.stream().mapToInt(Integer::intValue).sum();
int missing = expectedSum - actualSum;
System.out.println("Missing number: " + missing);
}
}
OUTPUT:
Missing number: 4
26. Determine whether a list contains any duplicate values.
import java.util.*;
import java.util.stream.Collectors;
public class CheckDuplicates {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 2, 5);
boolean hasDuplicates = numbers.stream()
.collect(Collectors.toSet())
.size() < numbers.size();
System.out.println("Contains duplicates? " + hasDuplicates);
}
}
OUTPUT:
Contains duplicates? true
27. From a list of integers, identify the longest consecutive sequence of numbers.
import java.util.*;
import java.util.stream.Collectors;
public class LongestConsecutiveSequence {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(100, 4, 200, 1, 3, 2);
Set<Integer> numSet = new HashSet<>(numbers);
int longestStreak = 0;
for (int num : numSet) {
if (!numSet.contains(num - 1)) {
int currentNum = num;
int streak = 1;
while (numSet.contains(currentNum + 1)) {
currentNum++;
streak++;
}
longestStreak = Math.max(longestStreak, streak);
}
}
System.out.println("Longest consecutive sequence length: " + longestStreak);
}
}
OUTPUT:
Longest consecutive sequence length: 4
28. Find all pairs of numbers in a list that add up to a specific target value.
import java.util.*;
import java.util.stream.IntStream;
public class PairsWithTargetSum {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 4, 3, 5, 7, 8, -1);
int target = 7;
IntStream.range(0, numbers.size())
.forEach(i -> IntStream.range(i + 1, numbers.size())
.filter(j -> numbers.get(i) + numbers.get(j) == target)
.forEach(j -> System.out.println(
"(" + numbers.get(i) + ", " + numbers.get(j) + ")")));
}
}
OUTPUT:
(2, 5)
(4, 3)
(8, -1)
Conclusion
In this blog post, we solved 8 advanced Java Stream API problems that are commonly asked in coding interviews. These problems tested your ability to work with multiple collections, detect missing numbers, handle duplicates, find patterns like consecutive sequences, and solve pair-based logic efficiently.
By practicing these exercises, you’ll gain the confidence to solve Real-World challenges where Java Streams are used for performance and clean code.
- Intermediate Java Stream API Coding Problems for Interview Preparation (11–20)
- Expert-Level Java Stream API Questions for Interview Success (Coming Soon)