Java Streams – Practice Questions
Contents
• Basics (Filter/Map/Distinct/Sort): 1–5
• Duplicates, frequency, flattening: 6–10
• Top-N / Min-Max / Partitioning: 11–13, 36–39
• Strings: 14, 40, 44–45, 47–50
• Employee / Department grouping: 15, 26–35, 43
• Skip/Limit/Aggregates & Match: 16–25, 41–42, 46
1) Filter even numbers
Collection: List<Integer> numbers
Input
numbers = [1, 2, 3, 4, 5, 6]
Code
List<Integer> result = [Link]().filter(n -> n % 2 == 0).toList();
Output
[2, 4, 6]
2) Convert to uppercase
Collection: List<String> words
Input
words = ["java", "spring"]
Code
List<String> result = [Link]().map(String::toUpperCase).toList();
Output
["JAVA", "SPRING"]
3) Find max number
Input
numbers = [10, 50, 30]
Code
int max = [Link]().max(Integer::compareTo).orElse(0);
Java Streams – Practice Questions
Output
50
4) Remove duplicates
Input
numbers = [1, 2, 2, 3, 3]
Code
List<Integer> result = [Link]().distinct().toList();
Output
[1, 2, 3]
5) Sort descending
Input
numbers = [5, 1, 3, 2]
Code
List<Integer> result = [Link]().sorted([Link]()).toList();
Output
[5, 3, 2, 1]
6) Find duplicates
Input
numbers = [1, 2, 3, 2, 3, 4]
Code
Set<Integer> seen = new HashSet<>();Set<Integer> duplicates = [Link]().filter(n ->
).collect([Link]());
Output
[2, 3]
Java Streams – Practice Questions
7) Frequency of elements
Input
numbers = [1, 2, 2, 3, 3, 3]
Code
Map<Integer, Long> freq = [Link]().collect([Link]([Link](),
[Link]()));
Output
{1=1, 2=2, 3=3}
8) Flatten nested list
Collection: List<List<Integer>>
Input
nested = [[1, 2], [3, 4]]
Code
List<Integer> flat = [Link]().flatMap(List::stream).toList();
Output
[1, 2, 3, 4]
9) Group strings by length
Input
words = ["cat", "dog", "apple"]
Code
Map<Integer, List<String>> result = [Link]().collect([Link](String::length));
Output
{3=["cat", "dog"], 5=["apple"]}
10) Convert list to map (square values)
Input
numbers = [1, 2, 3]
Code
Map<Integer, Integer> map = [Link]().collect([Link]([Link](), n -> n
* n));
Java Streams – Practice Questions
Output
{1=1, 2=4, 3=9}
11) Second highest number
Input
numbers = [10, 20, 30, 40]
Code
int secondHighest =
[Link]().distinct().sorted([Link]()).skip(1).findFirst().orElse(0);
Output
30
12) Top 3 numbers
Input
numbers = [5, 1, 9, 7, 3]
Code
List<Integer> top3 = [Link]().sorted([Link]()).limit(3).toList();
Output
[9, 7, 5]
13) Partition even/odd
Input
numbers = [1, 2, 3, 4]
Code
Map<Boolean, List<Integer>> result = [Link]().collect([Link](n -> n %
2 == 0));
Output
{true=[2, 4], false=[1, 3]}
14) First non-repeating character
Input
text = "stress"
Java Streams – Practice Questions
Code
char result = [Link]().mapToObj(c -> (char) c).collect([Link]([Link](),
LinkedHashMap::new, [Link]())).entrySet().stream().filter(e -> [Link]() ==
1).map([Link]::getKey).findFirst().orElse(null);
Output
't'
15) Highest salary per department
Collection: List<Employee>
Input
employees = [(IT, 3000), (IT, 5000), (HR, 2000)]
Code
Map<String, Optional<Employee>> result =
[Link]().collect([Link](Employee::getDept,
[Link]([Link](Employee::getSalary))));
Output
{IT=(5000), HR=(2000)}
16) Count numbers greater than 10
Collection: List<Integer> numbers
Input
numbers = [5, 12, 18, 2]
Code
long count = [Link]().filter(n -> n > 10).count();
Output
17) Get first 3 elements
Collection: List<Integer> numbers
Input
numbers = [1, 2, 3, 4, 5]
Code
List<Integer> result = [Link]().limit(3).toList();
Java Streams – Practice Questions
Output
[1, 2, 3]
18) Skip first 2 elements
Input
numbers = [1, 2, 3, 4, 5]
Code
List<Integer> result = [Link]().skip(2).toList();
Output
[3, 4, 5]
19) Average of numbers
Input
numbers = [10, 20, 30]
Code
double avg = [Link]().mapToInt(Integer::intValue).average().orElse(0);
Output
20.0
20) Find minimum
Input
numbers = [10, 5, 20]
Code
int min = [Link]().min(Integer::compareTo).orElse(0);
Output
21) Check if any number is even
Input
numbers = [1, 3, 5, 6]
Code
boolean result = [Link]().anyMatch(n -> n % 2 == 0);
Output
true
Java Streams – Practice Questions
22) Check if all numbers are positive
Input
numbers = [1, 2, 3]
Code
boolean result = [Link]().allMatch(n -> n > 0);
Output
true
23) Convert list to set
Input
numbers = [1, 2, 2, 3]
Code
Set<Integer> result = [Link]().collect([Link]());
Output
[1, 2, 3]
24) Sum using reduce
Input
numbers = [1, 2, 3, 4]
Code
int sum = [Link]().reduce(0, Integer::sum);
Output
10
25) Find square of each number
Input
numbers = [1, 2, 3]
Code
List<Integer> result = [Link]().map(n -> n * n).toList();
Output
[1, 4, 9]
Java Streams – Practice Questions
26) Get employee names
Input
employees = [(John, 3000), (Raj, 2000), (Amit, 5000)]
Code
List<String> result = [Link]().map(Employee::getName).toList();
Output
["John", "Raj", "Amit"]
27) Filter employees by department
Collection: List<Employee> employees
Input
employees = [(John, IT), (Raj, HR), (Amit, IT)]
Code
List<Employee> result = [Link]().filter(e -> [Link]().equals("IT")).toList();
Output
[(John, IT), (Amit, IT)]
28) Find highest salary employee
Input
employees = [(John, 3000), (Raj, 2000), (Amit, 5000)]
Code
Employee result =
[Link]().max([Link](Employee::getSalary)).orElse(null);
Output
(Amit, 5000)
29) Count employees per department
Input
employees = [(IT), (HR), (IT), (HR), (IT)]
Code
Map<String, Long> result = [Link]().collect([Link](Employee::getDept,
[Link]()));
Java Streams – Practice Questions
Output
{IT=3, HR=2}
30) Group employees by department
Input
employees = [(John, IT), (Raj, HR), (Amit, IT)]
Code
Map<String, List<Employee>> result =
[Link]().collect([Link](Employee::getDept));
Output
{IT=[(John), (Amit)], HR=[(Raj)]}
31) Total salary per department
Input
employees = [(IT, 1000), (IT, 2000), (HR, 1500)]
Code
Map<String, Integer> result =
[Link]().collect([Link](Employee::getDept,
[Link](Employee::getSalary)));
Output
{IT=3000, HR=1500}
32) Group employees by department and average salary
Input
employees = [(IT, 1000), (IT, 2000), (HR, 1500)]
Code
Map<String, Double> result =
[Link]().collect([Link](Employee::getDept,
[Link](Employee::getSalary)));
Output
{IT=1500.0, HR=1500.0}
33) Find employee names sorted by salary
Input
employees = [(John, 3000), (Raj, 2000), (Amit, 5000)]
Java Streams – Practice Questions
Code
List<String> result =
[Link]().sorted([Link](Employee::getSalary)).map(Employee::getNam
e).toList();
Output
["Raj", "John", "Amit"]
34) Find employees with salary > 2500
Input
employees = [(John, 3000), (Raj, 2000), (Amit, 5000)]
Code
List<Employee> result = [Link]().filter(e -> [Link]() > 2500).toList();
Output
[(John, 3000), (Amit, 5000)]
35) Convert employee list to map (name → salary)
Input
employees = [(John, 3000), (Raj, 2000)]
Code
Map<String, Integer> result = [Link]().collect([Link](Employee::getName,
Employee::getSalary));
Output
{John=3000, Raj=2000}
36) Second highest number
Collection: List<Integer> numbers
Input
numbers = [10, 20, 30, 40]
Code
int result =
[Link]().distinct().sorted([Link]()).skip(1).findFirst().orElse(0);
Output
30
Java Streams – Practice Questions
37) Third highest number
Input
numbers = [10, 20, 30, 40]
Code
int result =
[Link]().distinct().sorted([Link]()).skip(2).findFirst().orElse(0);
Output
20
38) Top 3 highest numbers
Input
numbers = [5, 1, 9, 7, 3]
Code
List<Integer> result =
[Link]().distinct().sorted([Link]()).limit(3).toList();
Output
[9, 7, 5]
39) Partition even and odd
Input
numbers = [1, 2, 3, 4]
Code
Map<Boolean, List<Integer>> result = [Link]().collect([Link](n -> n %
2 == 0));
Output
{true=[2, 4], false=[1, 3]}
40) First non-repeating character
Collection: String text
Input
text = "stress"
Code
Java Streams – Practice Questions
char result = [Link]().mapToObj(c -> (char) c).collect([Link]([Link](),
LinkedHashMap::new, [Link]())).entrySet().stream().filter(e -> [Link]() ==
1).map([Link]::getKey).findFirst().orElse(null);
Output
't'
41) Most frequent element
Input
numbers = [1, 2, 2, 3, 3, 3]
Code
Integer result = [Link]().collect([Link]([Link](),
[Link]())).entrySet().stream().max([Link]()).map([Link]::g
etKey).orElse(null);
Output
42) Flatten map values
Collection: Map<String, List<Integer>> map
Input
map = {A=[1, 2], B=[3, 4]}
Code
List<Integer> result = [Link]().stream().flatMap(List::stream).toList();
Output
[1, 2, 3, 4]
43) Highest salary per department
Collection: List<Employee> employees
Input
employees = [(IT, 3000), (IT, 5000), (HR, 2000)]
Code
Map<String, Optional<Employee>> result =
[Link]().collect([Link](Employee::getDept,
[Link]([Link](Employee::getSalary))));
Output
{IT=(5000), HR=(2000)}
Java Streams – Practice Questions
44) Word frequency
Collection: String sentence
Input
sentence = "cat dog cat"
Code
Map<String, Long> result = [Link]([Link]("
")).collect([Link]([Link](), [Link]()));
Output
{cat=2, dog=1}
45) Reverse each word
Input
sentence = "java stream"
Code
List<String> result = [Link]([Link](" ")).map(word -> new
StringBuilder(word).reverse().toString()).toList();
Output
["avaj", "maerts"]
46) Merge two maps
Collection: Map<String,Integer> map1, map2
Input
map1 = {A=10, B=20}
map2 = {A=5, C=15}
Code
[Link]((k, v) -> [Link](k, v, Integer::sum));
Output
{A=15, B=20, C=15}
47) Find longest string
Collection: List<String> words
Input
words = ["hi", "hello", "welcome"]
Java Streams – Practice Questions
Code
String result = [Link]().max([Link](String::length)).orElse(null);
Output
"welcome"
48) Find shortest string
Input
words = ["hi", "hello", "welcome"]
Code
String result = [Link]().min([Link](String::length)).orElse(null);
Output
"hi"
49) Concatenate all strings
Input
words = ["java", "stream"]
Code
String result = [Link]().reduce("", (a, b) -> a + b);
Output
"javastream"
50) Count characters in string
Input
text = "java"
Code
long count = [Link]().count();
Output