In this blog post, we’ll solve 10 intermediate-level coding problems on Integer collections using the Java Stream API, each with a separate program and output. These problems are commonly asked in interviews and will strengthen your functional programming skills.
If you haven’t already checked out the first part of this series covering Top 10 Basic Stream API Logical Interview Questions on Integer Collections (1–10), make sure to visit that blog post first. It will help you build a strong foundation before moving on to these intermediate-level problems.
Intermediate Level Java Stream API Coding Problems
11. From a List of Integers, Find All Prime Numbers
Program:
import java.util.*;
import java.util.stream.*;
public class PrimeNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 3, 4, 5, 10, 11, 15, 17, 20);
List<Integer> primes = numbers.stream()
.filter(PrimeNumbers::isPrime)
.collect(Collectors.toList());
System.out.println("Prime Numbers: " + primes);
}
private static boolean isPrime(int num) {
if (num <= 1) return false;
return IntStream.rangeClosed(2, (int)Math.sqrt(num))
.allMatch(n -> num % n != 0);
}
}
OUTPUT:
Prime Numbers: [2, 3, 5, 11, 17]
12. Extract the First 5 Even Numbers from a Collection
Program:
import java.util.*;
import java.util.stream.*;
public class FirstFiveEvens {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10, 12, 14);
List<Integer> firstFive = numbers.stream()
.filter(n -> n % 2 == 0)
.limit(5)
.collect(Collectors.toList());
System.out.println("First 5 Even Numbers: " + firstFive);
}
}
OUTPUT:
First 5 Even Numbers: [2, 4, 6, 8, 10]
13. Convert All Integers into Their Squared Values
Program:
import java.util.*;
import java.util.stream.*;
public class SquareNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squared = numbers.stream()
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println("Squared Values: " + squared);
}
}
OUTPUT:
Squared Values: [1, 4, 9, 16, 25]
14. Calculate the Total of All Even Numbers
Program:
import java.util.*;
import java.util.stream.*;
public class SumEvenNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 5, 8, 11, 14, 17);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();
System.out.println("Sum of Even Numbers: " + sum);
}
}
OUTPUT:
Sum of Even Numbers: 24
15. Check If Every Number in a List Is Positive
Program:
import java.util.*;
import java.util.stream.*;
public class AllPositiveCheck {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
boolean allPositive = numbers.stream().allMatch(n -> n > 0);
System.out.println("All Positive? " + allPositive);
}
}
OUTPUT:
All Positive? true
16. Verify Whether Any Negative Number Exists
Program:
import java.util.*;
import java.util.stream.*;
public class AnyNegativeCheck {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(3, -2, 7, 5, -8, 10);
boolean hasNegative = numbers.stream().anyMatch(n -> n < 0);
System.out.println("Contains Negative? " + hasNegative);
}
}
OUTPUT:
Contains Negative? true
17. Calculate the Product of All Numbers
Program:
import java.util.*;
import java.util.stream.*;
public class ProductNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 3, 4, 5);
int product = numbers.stream()
.reduce(1, (a, b) -> a * b);
System.out.println("Product of Numbers: " + product);
}
}
OUTPUT:
Product of Numbers: 120
18. Identify the Most Frequent Number
Program:
import java.util.*;
import java.util.function.Function;
import java.util.stream.*;
public class MostFrequentNumber {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(4, 5, 6, 5, 4, 5, 7, 6, 5);
int mostFrequent = numbers.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.max(Map.Entry.comparingByValue())
.get().getKey();
System.out.println("Most Frequent Number: " + mostFrequent);
}
}
OUTPUT:
Most Frequent Number: 5
19. Divide a List into Even and Odd Groups
Program:
import java.util.*;
import java.util.stream.*;
public class PartitionEvenOdd {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 3, 4, 5, 6, 7, 8);
Map<Boolean, List<Integer>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
System.out.println("Even Numbers: " + partitioned.get(true));
System.out.println("Odd Numbers: " + partitioned.get(false));
}
}
OUTPUT:
Even Numbers: [2, 4, 6, 8]
Odd Numbers: [3, 5, 7]
20. Find the nth Largest Element
Program:
import java.util.*;
import java.util.stream.*;
public class NthLargestElement {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 30, 20, 50, 40, 60);
int n = 3; // 3rd largest
int nthLargest = numbers.stream()
.sorted(Comparator.reverseOrder())
.skip(n - 1)
.findFirst()
.orElseThrow();
System.out.println(n + "rd Largest Element: " + nthLargest);
}
}
OUTPUT:
3rd Largest Element: 40
Conclusion
In this blog post, we explored 10 intermediate-level Java Stream API interview questions (11–20) using integer collections. These problems focused on slightly more complex use cases such as prime number detection, grouping, verifying conditions across collections, and calculating advanced metrics like frequency and nth largest elements.
If you missed the first blog post (1–10 basic questions), I recommend checking it out here: Top 10 Basic Stream API Logical Interview Questions on Integer Collections. It lays the groundwork for these intermediate challenges and ensures you’re progressing step by step.