Java Streams Interview Questions

By Pradyumna Chippigiri

February 17, 2026


I was preparing for a Java Developer interview, so thought of making this a place to come back and revise when I forget something.

1. Convert list of strings to uppercase, sort them and print them.

JAVA CODE
1import java.util.*;
2
3List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana");
4fruits.stream().map(String::toUpperCase).sorted().forEach(f -> System.out.println(f));

2. Get the list of strings where the length of each string is greater than 5.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5List<String> result = fruits.stream()
6 .filter(s -> s.length() > 5)
7 .collect(Collectors.toList());
8
9System.out.println(result);

3. Remove duplicates from a list of strings.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "mango","apple", "banana", "pineapple", "cherry");
5List<String> result = fruits.stream()
6 .distinct()
7 .collect(Collectors.toList()); // remove duplicates
8
9System.out.println(result);

4. Get the list of strings having a particular word.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "mango","apple", "banana", "pineapple", "cherry");
5List<String> result = fruits.stream()
6 .filter(s -> s.contains("apple"))
7 .collect(Collectors.toList()); // get strings containing "apple"
8
9System.out.println(result);

5. Remove strings starting with 'a'.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "mango","apple", "banana", "pineapple", "cherry");
5List<String> result = fruits.stream()
6 .filter(s -> !s.startsWith("a"))
7 .collect(Collectors.toList());
8
9System.out.println(result);

6.Find first element in the list that starts with 'a'.

findFirst() is a short circuiting terminal operation, it returns an Optional<T> object.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> names = Arrays.asList("pushkar", "praddy", "nachu", "arjit", "mamata");
5Optional<String> res = names.stream()
6 .filter(s -> s.startsWith("a")) // find first string starting with "a"
7 .findFirst(); // find first element
8
9res.ifPresent(System.out::println); // prints only if present
10System.out.println(res.orElse("No element found")); // if present, print the element, otherwise print "No element found"

7. Length of each string in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5List<Integer> result = fruits.stream()
6 .map(String::length)
7 .collect(Collectors.toList());
8
9System.out.println(result);

8. Find the longest word in the list

max() is a terminal operation, it returns an Optional<T> object.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5Optional<String> longest = fruits.stream()
6 .max(Comparator.comparingInt(String::length));
7
8System.out.println(longest.orElse("No element found"));

9. Sort the list of strings, based on their lengths in ascending

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5List<String> result = fruits.stream()
6 .sorted(Comparator.comparingInt(String::length))
7 .collect(Collectors.toList());
8
9System.out.println(result);

10. Sort the list of strings, based on their lengths in descending order.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5List<String> result = fruits.stream()
6 .sorted(Comparator.comparingInt(String::length).reversed())
7 .collect(Collectors.toList());
8
9System.out.println(result);

11. Get the list of strings having vowels.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5List<String> result = fruits.stream()
6 .filter(s -> s.matches(".*[aeiou].*"))
7 .collect(Collectors.toList());
8
9System.out.println(result);

12. Convert a list of strings into a string seperated by commas.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> fruits = Arrays.asList("cherry", "mango", "apple", "banana", "pineapple");
5String result = fruits.stream()
6 .collect(Collectors.joining(","));
7
8System.out.println(result);

13. Given a list of strings, group by the first letter of each string.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> names = Arrays.asList("pushkar", "praddy", "nachu", "arjit", "mamata");
5
6Map<Character, List<String>> result = names.stream()
7 .collect(Collectors.groupingBy(s -> s.charAt(0)));
8
9System.out.println(result);

14. Remove all strings in the list which are null or empty.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> names = Arrays.asList("pushkar", "praddy", "nachu", "arjit", "mamata");
5List<String> result = names.stream()
6 .filter(s -> s != null && !s.isEmpty())
7 .collect(Collectors.toList());
8
9System.out.println(result);

15. Given a list of strings, group it by the length of the string.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> names = Arrays.asList("pushkar", "praddy", "nachu", "arjit", "mamata");
5Map<Integer, List<String>> result = names.stream()
6 .collect(Collectors.groupingBy(String::length));
7
8System.out.println(result);

16. Given a list of strings, group by the list of strings with vowels and no vowels into 2 lists.

JAVA CODE
1import java.util.*;
2import java.util.function.Predicate;
3import java.util.stream.Collectors;
4
5Predicate<String> hasVowel = s -> s.toLowerCase().matches(".*[aeiou].*");
6
7List<String> words = Arrays.asList("sky", "rhythm", "apple", "myth", "banana", "crypt");
8
9Map<Boolean, List<String>> res = words.stream()
10 .collect(Collectors.partitioningBy(hasVowel));
11
12List<String> withVowels = res.get(true);
13List<String> noVowels = res.get(false);
14
15System.out.println(withVowels);
16System.out.println(noVowels);

17. Given a list of strings, find the average length of the strings.

average() is a terminal operation, it only works on numeric streams.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<String> names = Arrays.asList("pushkar", "praddy", "nachu", "arjit", "mamata");
5Double average = names.stream()
6 .mapToInt(String::length)
7 .average()
8 .orElse(0.0);
9System.out.println(average);

Streams for numbers

1. Get the even numbers in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5List<Integer> result = numbers.stream()
6 .filter(n -> n % 2 == 0)
7 .collect(Collectors.toList());
8
9System.out.println(result);

2. Sort a list of integers in ascending order.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5List<Integer> result = numbers.stream()
6 .sorted()
7 .collect(Collectors.toList());
8
9System.out.println(result);

3. Sort a list of integers in descending order.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5List<Integer> result = numbers.stream()
6 .sorted(Comparator.reverseOrder())
7 .collect(Collectors.toList());
8
9System.out.println(result);

4. Find the sum of all the numbers in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5Integer sum = numbers.stream().mapToInt(Integer::intValue).sum();
6System.out.println(sum);

5. Find the average of the numbers in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5Double average = numbers.stream().mapToInt(Integer::intValue).average().orElse(0.0);
6System.out.println(average);

6. Find the maximum number in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5Integer max = numbers.stream().mapToInt(Integer::intValue).max().orElse(0);
6System.out.println(max);

7. Find the minimum number in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5Integer min = numbers.stream().mapToInt(Integer::intValue).min().orElse(0);
6System.out.println(min);

8. Remove duplicates from the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5List<Integer> result = numbers.stream().distinct().collect(Collectors.toList());
6System.out.println(result);

9. Convert a list of integers to a list of their squares and print them.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5List<Integer> result = numbers.stream().map(n -> n * n).collect(Collectors.toList());
6System.out.println(result);

10. Find the sum of the squares of the numbers in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5Integer sum = numbers.stream().map(n -> n * n).mapToInt(Integer::intValue).sum();
6System.out.println(sum);

11. Find second largest number in the list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
5Integer secondLargest = numbers.stream().sorted(Comparator.reverseOrder()).skip(1).findFirst().orElse(0);
6System.out.println(secondLargest);

12. Filter map entries by value

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4Map<String, Integer> map = new HashMap<>();
5map.put("A", 10);
6map.put("B", 20);
7map.put("C", 30);
8
9map.entrySet().stream()
10 .filter(entry -> entry.getValue() > 15)
11 .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));

13. Sum all values in a map.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4Map<String, Integer> map = new HashMap<>();
5map.put("A", 10);
6map.put("B", 20);
7map.put("C", 30);
8
9Integer sum = map.values().stream().mapToInt(Integer::intValue).sum();
10System.out.println(sum);

14. Given a Map<String, List<Integer>>, use streams to create a new Map<String, Integer> where each key maps to the maximum value from its list.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4Map<String, List<Integer>> input = new HashMap<>();
5input.put("A", Arrays.asList(1, 5, 3));
6input.put("B", Arrays.asList(10, 2, 8));
7input.put("C", Arrays.asList(7, 9, 4));
8
9Map<String, Integer> result = input.entrySet().stream()
10 .collect(Collectors.toMap(
11 Map.Entry::getKey,
12 entry -> Collections.max(entry.getValue())
13 ));
14
15System.out.println(result);

Stream APIs on User defined objects

Given a list of employee objects where each employee has properties empName, empId, empCity, salary, dept.

Run the setup block once to see the shared model/data. Other runnable blocks only show stream logic, but they still execute with this setup in the background.

JAVA CODE - SETUP (REFERENCE)
1import java.util.*;
2
3class Employee {
4 private final String empName;
5 private final int empId;
6 private final String empCity;
7 private final double salary;
8 private final String dept;
9
10 Employee(String empName, int empId, String empCity, double salary, String dept) {
11 this.empName = empName;
12 this.empId = empId;
13 this.empCity = empCity;
14 this.salary = salary;
15 this.dept = dept;
16 }
17
18 public String getEmpName() { return empName; }
19 public int getEmpId() { return empId; }
20 public String getEmpCity() { return empCity; }
21 public double getSalary() { return salary; }
22 public String getDept() { return dept; }
23}
24
25List<Employee> employees = new ArrayList<>();
26employees.add(new Employee("John", 101, "New York", 75000, "IT"));
27employees.add(new Employee("Jane", 102, "Los Angeles", 300000, "HR"));
28employees.add(new Employee("Jim", 103, "New York", 90000, "IT"));

1. Find all Employees who work in "New York" and get the count.

JAVA CODE
1Long count = employees.stream().filter(e -> e.getEmpCity().equals("New York")).count();
2System.out.println(count);

2. Find all employees where salary is greater than 100000.

JAVA CODE
1List<Employee> highSalaryEmployees = employees.stream()
2 .filter(e -> e.getSalary() > 100000)
3 .toList();
4System.out.println(highSalaryEmployees.size());

3. Print all employee names in upper case and alphabetical order.

JAVA CODE
1employees.stream()
2 .map(Employee::getEmpName.toUpperCase())
3 .sorted()
4 .forEach(System.out::println);

4. Get the first employee whos department is "IT".

JAVA CODE
1Employee firstITEmployee = employees.stream()
2 .filter(e -> e.getDept().equals("IT"))
3 .findFirst()
4 .orElse(null);
5
6System.out.println(firstITEmployee.getEmpName());

5. Get total salary of all employees in the "IT" department.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3Double totalSalary = employees.stream()
4 .filter(e -> e.getDept().equals("IT"))
5 .mapToDouble(Employee::getSalary)
6 .sum();
7
8System.out.println(totalSalary);

6. Get a map of employee names and their salaries.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4Map<String, Double> employeeSalaries = employees.stream()
5 .collect(Collectors.toMap(Employee::getEmpName, Employee::getSalary));
6
7System.out.println(employeeSalaries);

7. Get employees with highest salary.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Employee> highestSalaryEmployees = employees.stream()
5 .sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
6 .limit(1)
7 .toList();
8
9System.out.println(highestSalaryEmployees.get(0).getEmpName());

8. Get employees with second highest salary.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4List<Employee> secondHighestSalaryEmployees = employees.stream()
5 .sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
6 .skip(1)
7 .limit(1)
8 .toList();
9
10System.out.println(secondHighestSalaryEmployees.get(0).getEmpName());

9. Get employees earning above average salary.

JAVA CODE
1import java.util.*;
2import java.util.stream.Collectors;
3
4Double averageSalary = employees.stream()
5 .mapToDouble(Employee::getSalary)
6 .average()
7 .orElse(0.0);
8
9List<Employee> aboveAverageSalaryEmployees = employees.stream()
10 .filter(e -> e.getSalary() > averageSalary)
11 .toList();

10. Get the employee with the longest name

JAVA CODE
1Employee longestNameEmployee = employees.stream()
2 .max(Comparator.comparingInt(e -> e.getEmpName().length()))
3 .orElse(null);
4
5System.out.println(longestNameEmployee.getEmpName());