Stream API Collector Problems: Java’s Stream API isn’t just about simple filtering or mapping; it’s also widely used with collectors for solving real-world programming challenges. Collectors provide advanced capabilities like grouping, partitioning, mapping, and reducing results in flexible ways. In this post, we will explore real-world Java Stream API collector problems on integer collections that are frequently asked in coding interviews.
If you missed the previous blog posts covering 28 other important Stream API questions, scroll to the bottom of this post for quick navigation.
Java Stream API Collector Problems
29. From a list, find numbers that appear more than once
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DuplicateNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 3, 7, 5, 9, 3, 4, 7, 10);
Set<Integer> duplicates = numbers.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
System.out.println("Duplicate numbers: " + duplicates);
}
}
OUTPUT:
Duplicate numbers: [3, 5, 7]
30. Group numbers into two categories: even and odd
import java.util.*;
import java.util.stream.Collectors;
public class GroupEvenOdd {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(12, 7, 3, 18, 25, 42, 9, 10);
Map<Boolean, List<Integer>> grouped = numbers.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
System.out.println("Even numbers: " + grouped.get(true));
System.out.println("Odd numbers: " + grouped.get(false));
}
}
OUTPUT:
Even numbers: [12, 18, 42, 10]
Odd numbers: [7, 3, 25, 9]
31. From a collection, find the top three highest numbers
import java.util.*;
import java.util.stream.Collectors;
public class TopThreeNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(50, 10, 70, 30, 90, 20, 80);
List<Integer> topThree = numbers.stream()
.sorted(Comparator.reverseOrder())
.limit(3)
.collect(Collectors.toList());
System.out.println("Top three highest numbers: " + topThree);
}
}
OUTPUT:
Top three highest numbers: [90, 80, 70]
32. Convert a list of integers into a mapping where each number is associated with its square
import java.util.*;
import java.util.stream.Collectors;
public class NumberSquareMap {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 3, 5, 7, 10);
Map<Integer, Integer> squareMap = numbers.stream()
.collect(Collectors.toMap(n -> n, n -> n * n));
System.out.println("Number-Square Map: " + squareMap);
}
}
OUTPUT:
Number-Square Map: {2=4, 3=9, 5=25, 7=49, 10=100}
33. Find the first number in a list that does not repeat
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FirstNonRepeating {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(4, 5, 7, 4, 5, 9, 7, 11, 13);
Integer firstNonRepeating = numbers.stream()
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() == 1)
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
System.out.println("First non-repeating number: " + firstNonRepeating);
}
}
OUTPUT:
First non-repeating number: 9
34. Verify whether a list of integers is sorted in ascending order
import java.util.*;
import java.util.stream.IntStream;
public class CheckSorted {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 5, 7, 9, 11);
boolean isSorted = IntStream.range(0, numbers.size() - 1)
.allMatch(i -> numbers.get(i) <= numbers.get(i + 1));
System.out.println("Is the list sorted in ascending order? " + isSorted);
}
}
OUTPUT:
Is the list sorted in ascending order? true
Conclusion
In this blog post, we explored real-world Stream API collector problems on integer collections, such as finding duplicates, grouping numbers, mapping with squares, and verifying sorting. These types of collector-based challenges are commonly asked in Java interviews. Mastering these problems ensures you are comfortable using Collectors
, partitioningBy
, groupingBy
, and other advanced techniques with the Stream API.
If you want to go through the previous blog post of this series, check out the links below:
- Top 10 Basic Stream API Logical Interview Questions on Integer Collections
- Intermediate Java Stream API Coding Problems for Interview Preparation
- Advanced Java Stream API Logical Programs Frequently Asked in Interviews