Java Streams Interview Questions
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 duplicates8 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 element8 9res.ifPresent(System.out::println); // prints only if present10System.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);