Advanced Java Stream API Logical Programs Frequently Asked in Interviews

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:

21. From a list, remove all numbers greater than 100.

Java
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.

Java
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.

Java
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.

Java
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.

Java
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.

Java
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.

Java
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.

Java
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.

Java logical programs list


Java Basic Programs

Java String Programs

Java String Array Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Java Programs based on Stream API (Java 8)

Leave a Reply

Your email address will not be published. Required fields are marked *