Complete Java Streams Interview Guide (50
Questions + Solutions)
This guide contains commonly asked Java Streams interview questions including real-world
patterns, edge cases, and employee/map/string based problems. All examples use lambda
expressions and focus on patterns frequently asked in technical interviews.
Section 1: Basic Stream Operations (Questions 1–10)
1. Filter even numbers from a list
[Link]()
.filter(n -> n % 2 == 0)
.forEach(n -> [Link](n));
2. Convert list of strings to uppercase
[Link]()
.map(s -> [Link]())
.collect([Link]());
3. Count elements in a stream
long count = [Link]().count();
4. Find maximum number
int max = [Link]()
.max((a,b) -> [Link](b))
.get();
5. Find minimum number
int min = [Link]()
.min((a,b) -> [Link](b))
.get();
6. Sort numbers
[Link]()
.sorted((a,b) -> a-b)
.collect([Link]());
7. Remove duplicates
[Link]()
.distinct()
.collect([Link]());
8. Sum of numbers
int sum = [Link]()
.mapToInt(n -> n)
.sum();
9. Average value
double avg = [Link]()
.mapToInt(n -> n)
.average()
.getAsDouble();
10. First element
Optional<Integer> first = [Link]().findFirst();
Section 2: Intermediate Problems (Questions 11–20)
11. Numbers greater than 50
[Link]()
.filter(n -> n > 50)
.collect([Link]());
12. Top 3 highest numbers
[Link]()
.sorted((a,b) -> b-a)
.limit(3)
.collect([Link]());
13. Second highest number
[Link]()
.distinct()
.sorted((a,b) -> b-a)
.skip(1)
.findFirst()
.get();
14. Join strings
String result = [Link]()
.collect([Link](", "));
15. Partition even and odd
Map<Boolean,List<Integer>> map =
[Link]()
.collect([Link](n -> n % 2 == 0));
16. Frequency of elements
Map<Integer,Long> freq =
[Link]()
.collect([Link](n -> n, [Link]()));
17. Sort strings by length
[Link]()
.sorted((a,b) -> [Link]() - [Link]())
.collect([Link]());
18. Longest string
[Link]()
.max((a,b) -> [Link]() - [Link]())
.get();
19. Flatten nested list
[Link]()
.flatMap(l -> [Link]())
.collect([Link]());
20. Merge two lists
[Link]([Link](), [Link]())
.collect([Link]());
Section 3: String Based Problems (Questions 21–30)
21. Count vowels in string
long count = [Link]()
.mapToObj(c -> (char)c)
.filter(c -> "aeiouAEIOU".indexOf(c) != -1)
.count();
22. Reverse a string
String reversed =
[Link]()
.mapToObj(c -> (char)c)
.reduce("", (a,b) -> b + a, (a,b) -> b + a);
23. Find duplicate characters
Set<Character> set = new HashSet<>();
[Link]()
.mapToObj(c -> (char)c)
.filter(c -> )
.forEach(c -> [Link](c));
24. First non■repeated character
[Link]()
.mapToObj(c -> (char)c)
.collect([Link](c -> c, LinkedHashMap::new, [Link]()))
.entrySet()
.stream()
.filter(e -> [Link]()==1)
.findFirst()
.get()
.getKey();
25. Check palindrome
boolean palindrome =
[Link](
[Link]()
.mapToObj(c -> (char)c)
.reduce("", (a,b)->b+a)
);
26. Count characters
Map<Character,Long> map =
[Link]()
.mapToObj(c -> (char)c)
.collect([Link](c -> c, [Link]()));
27. Remove duplicate characters
[Link]()
.distinct()
.mapToObj(c -> [Link]((char)c))
.collect([Link]());
28. Sort characters
[Link]()
.sorted()
.mapToObj(c -> [Link]((char)c))
.collect([Link]());
29. Convert string to char list
List<Character> list =
[Link]()
.mapToObj(c -> (char)c)
.collect([Link]());
30. Find most frequent character
[Link]()
.mapToObj(c -> (char)c)
.collect([Link](c -> c, [Link]()))
.entrySet()
.stream()
.max((a,b) -> [Link]().compareTo([Link]()))
.get();
Section 4: Employee / Real Interview Patterns (Questions 31–50)
31. Group employees by department
[Link]()
.collect([Link](e -> [Link]()));
32. Count employees per department
[Link]()
.collect([Link](
e -> [Link](),
[Link]()
));
33. Highest salary employee
[Link]()
.max((a,b)->[Link]([Link](),[Link]()))
.get();
34. Average salary
[Link]()
.mapToDouble(e->[Link]())
.average()
.getAsDouble();
35. Employees with salary > 50000
[Link]()
.filter(e -> [Link]() > 50000)
.collect([Link]());
36. Total salary expense
[Link]()
.mapToDouble(e->[Link]())
.sum();
37. Sort employees by salary
[Link]()
.sorted((a,b)->[Link]([Link](),[Link]()))
.collect([Link]());
38. Top 5 highest paid employees
[Link]()
.sorted((a,b)->[Link]([Link](),[Link]()))
.limit(5)
.collect([Link]());
39. Partition employees by salary > 50k
[Link]()
.collect([Link](e->[Link]()>50000));
40. Get employee names
[Link]()
.map(e->[Link]())
.collect([Link]());
41. Department names set
[Link]()
.map(e->[Link]())
.collect([Link]());
42. Map employee name to salary
[Link]()
.collect([Link](e->[Link](), e->[Link]()));
43. Highest salary per department
[Link]()
.collect([Link](
e->[Link](),
[Link]((a,b)->[Link]([Link](),[Link]()))
));
44. Average salary per department
[Link]()
.collect([Link](
e->[Link](),
[Link](e->[Link]())
));
45. Count employees per department
[Link]()
.collect([Link](
e->[Link](),
[Link]()
));
46. Employees sorted by name
[Link]()
.sorted((a,b)->[Link]().compareTo([Link]()))
.collect([Link]());
47. Find duplicate employee names
Set<String> set = new HashSet<>();
[Link]()
.map(e->[Link]())
.filter(n->)
.forEach(n->[Link](n));
48. First employee
[Link]().findFirst();
49. Skip first 5 employees
[Link]()
.skip(5)
.collect([Link]());
50. Limit employees to first 10
[Link]()
.limit(10)
.collect([Link]());