Stream Questions - Beginner
1. Find even numbers from a list
List<Integer> evens = list.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
2. Count words in a list
long count = words.stream()
.count();
3. Convert strings to uppercase
List<String> upper = words.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
4. Sum all numbers
int sum = numbers.stream()
.mapToInt(Integer::intValue)
.sum();
5. Remove duplicates
List<Integer> unique = list.stream()
.distinct()
.collect(Collectors.toList());
6. Find max
Optional<Integer> max = list.stream()
.max(Integer::compareTo);
7. Find min
Optional<Integer> min = list.stream()
.min(Integer::compareTo);
8. Sort a list
List<String> sorted = list.stream()
.sorted()
.collect(Collectors.toList());
9. Sort descending
List<Integer> sorted = list.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
10. Join strings
String joined = list.stream()
.collect(Collectors.joining(", "));
11. Filter strings starting with 'A'
List<String> result = list.stream()
.filter(s -> s.startsWith("A"))
.collect(Collectors.toList());
12. Get average
OptionalDouble avg = list.stream()
.mapToInt(Integer::intValue)
.average();
13. Group by string length
Map<Integer, List<String>> grouped = list.stream()
.collect(Collectors.groupingBy(String::length));
14. Count each element
Map<String, Long> freq = list.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
15. Find palindrome strings
List<String> palindromes = list.stream()
.filter(s -> s.equals(new StringBuilder(s).reverse().toString()))
.collect(Collectors.toList());
16. Find common elements in two lists
List<String> common = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
17. Convert list of strings to list of integers
List<Integer> nums = list.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());
18. Partition list into even and odd
Map<Boolean, List<Integer>> partitioned = list.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
19. Skip first 5 elements
List<Integer> skipped = list.stream()
.skip(5)
.collect(Collectors.toList());
20. Limit to first 3 elements
List<Integer> firstThree = list.stream()
.limit(3)
.collect(Collectors.toList());
21. Remove null values
List<String> filtered = list.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
22. Flatten nested list
List<Integer> flat = listOfLists.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
23. Create map from list
Map<Integer, String> map = list.stream()
.collect(Collectors.toMap(String::length, Function.identity(), (a, b) -> a));
24. Find first element
Optional<String> first = list.stream()
.findFirst();
25. Check if all are even
boolean allEven = list.stream()
.allMatch(n -> n % 2 == 0);
26. Check if any element is empty
boolean anyEmpty = list.stream()
.anyMatch(String::isEmpty);
27. Count characters in a string list
int totalLength = list.stream()
.mapToInt(String::length)
.sum();
28. Convert a list to a Set
Set<String> set = list.stream()
.collect(Collectors.toSet());
29. Sort list by string length
List<String> sorted = list.stream()
.sorted(Comparator.comparing(String::length))
.collect(Collectors.toList());
30. Reverse sort by length
List<String> sorted = list.stream()
.sorted(Comparator.comparing(String::length).reversed())
.collect(Collectors.toList());
31. Find duplicates
Set<String> seen = new HashSet<>();
Set<String> duplicates = list.stream()
.filter(e -> !seen.add(e))
.collect(Collectors.toSet());
32. Find longest string
Optional<String> longest = list.stream()
.max(Comparator.comparingInt(String::length));
33. Multiply all numbers
int product = list.stream()
.reduce(1, (a, b) -> a * b);
34. Count vowels in list
long vowels = list.stream()
.flatMapToInt(String::chars)
.mapToObj(c -> (char) c)
.filter(c -> "aeiouAEIOU".indexOf(c) != -1)
.count();
35. Find second highest
Optional<Integer> secondHighest = list.stream()
.sorted(Comparator.reverseOrder())
.distinct()
.skip(1)
.findFirst();
36. Frequency map of characters in string
Map<Character, Long> freq = str.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
37. Find repeated characters
Set<Character> seen = new HashSet<>();
Set<Character> repeated = str.chars()
.mapToObj(c -> (char) c)
.filter(c -> !seen.add(c))
.collect(Collectors.toSet());
38. Remove empty strings
List<String> filtered = list.stream()
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
39. Count digit characters in string
long digits = str.chars()
.filter(Character::isDigit)
.count();
40. Get list of squares
List<Integer> squares = list.stream()
.map(n -> n * n)
.collect(Collectors.toList());
41. Get distinct squares
List<Integer> squares = list.stream()
.map(n -> n * n)
.distinct()
.collect(Collectors.toList());
42. Sum of squares
int sumSquares = list.stream()
.map(n -> n * n)
.reduce(0, Integer::sum);
43. Find first non-repeating string
Optional<String> result = list.stream()
.filter(s -> Collections.frequency(list, s) == 1)
.findFirst();
44. Convert int[] to List<Integer>
List<Integer> result = Arrays.stream(arr)
.boxed()
.collect(Collectors.toList());
45. Convert List<Integer>
to int[]
int[] arr = list.stream()
.mapToInt(i -> i)
.toArray();
46. Find common prefix
String prefix = list.stream()
.reduce((a, b) -> {
int i = 0;
while (i < a.length() && i < b.length() && a.charAt(i) == b.charAt(i)) i++;
return a.substring(0, i);
}).orElse("");
47. Find longest word
Optional<String> longest = list.stream()
.max(Comparator.comparingInt(String::length));
48. Count words starting with uppercase
long count = list.stream()
.filter(s -> !s.isEmpty() && Character.isUpperCase(s.charAt(0)))
.count();
49. Print all elements
list.stream().forEach(System.out::println);
50. Convert map values to list
List<String> values = map.values().stream()
.collect(Collectors.toList());