50 Java Stream Scenario based Programs with Explanations
50 Java Stream Scenario based Programs with Explanations
int k = 3;
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.sorted(Map.Entry.<Integer, Long>comparingByValue().reversed())
.limit(k)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find the k most frequent elements in a list.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Collectors.summarizingInt(String::length);
Explanation:
This problem demonstrates how to use Java Streams for: implement a custom collector to get summary
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
the use case.
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.distinct()
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find all distinct permutations of characters in each
string.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
4. Batch process a large stream in fixed-size chunks (e.g., 100 items each)
Explanation:
This problem demonstrates how to use Java Streams for: batch process a large stream in fixed-size chunks
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
Explanation:
This problem demonstrates how to use Java Streams for: create a time-windowed stream from timestamped
events.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
if (i == lis.size()) lis.add(num);
Explanation:
This problem demonstrates how to use Java Streams for: find the longest increasing subsequence using
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
7. Group by multiple fields (e.g., department and designation)
e.getDesignation())));
Explanation:
This problem demonstrates how to use Java Streams for: group by multiple fields (e.g., department and
designation).
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
8. Flatten a hierarchical tree structure into a list using recursion and stream
return Stream.concat(Stream.of(node),
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: flatten a hierarchical tree structure into a list using
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Collectors.counting()));
Explanation:
This problem demonstrates how to use Java Streams for: parallel stream processing with thread-safe
accumulation.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.filter(isManager.and(earnsAbove50k))
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: custom stream-based dsl for filtering dynamic
criteria.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.filter(list2::contains)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find common elements from two lists using
streams.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.toMap(Function.identity(), String::length));
Explanation:
This problem demonstrates how to use Java Streams for: convert a list of strings to a map with string lengths
as values.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Explanation:
This problem demonstrates how to use Java Streams for: calculate the product of all integers in a list.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
14. Partition students based on pass/fail using score > 40
Explanation:
This problem demonstrates how to use Java Streams for: partition students based on pass/fail using score >
40.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.sorted(Comparator.comparing(Employee::getDepartment)
.thenComparing(Employee::getSalary, Comparator.reverseOrder()))
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: sort a list of employees by department then salary
descending.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.map(Employee::getName)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: get all manager names reporting to a particular
department.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(Book::getAuthor,
Collectors.groupingBy(Book::getGenre)));
Explanation:
This problem demonstrates how to use Java Streams for: group books by author and then by genre.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Collectors.counting()));
Explanation:
This problem demonstrates how to use Java Streams for: count files by file extension.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
the use case.
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(Purchase::getCustomer, Collectors.counting()))
.entrySet().stream()
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find customers with more than 3 purchases.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: convert a list of dates to strings in dd-mm-yyyy
format.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: filter out non-prime numbers using streams.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.max(Comparator.comparingInt(String::length))
.orElse("");
Explanation:
This problem demonstrates how to use Java Streams for: find the longest string in a list.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Explanation:
This problem demonstrates how to use Java Streams for: calculate moving average with window size 3.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find all palindromes in a list.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(Employee::getCity,
Explanation:
This problem demonstrates how to use Java Streams for: group employees by city and then by team size.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
.map(String::toLowerCase)
.distinct()
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: normalize and deduplicate emails.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
27. Get all students whose names start and end with vowels
.map(Student::getName)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: get all students whose names start and end with
vowels.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
28. Sum the values of a nested list structure
.flatMap(Collection::stream)
.mapToInt(Integer::intValue)
.sum();
Explanation:
This problem demonstrates how to use Java Streams for: sum the values of a nested list structure.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.map(Transaction::getDate)
.min(LocalDate::compareTo);
.map(Transaction::getDate)
.max(LocalDate::compareTo);
Explanation:
This problem demonstrates how to use Java Streams for: get the earliest and latest transaction dates.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.joining(", "));
Explanation:
This problem demonstrates how to use Java Streams for: convert list of objects to comma-separated values
for a field.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.boxed()
str.substring(i, j)))
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: generate stream of all substrings of a string.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Explanation:
This problem demonstrates how to use Java Streams for: create a frequency map of digit counts in a list of
numbers.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
33. Partition strings by length: short (<5), medium (5-10), long (>10)
"medium" : "long"));
Explanation:
This problem demonstrates how to use Java Streams for: partition strings by length: short (<5), medium
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find repeating characters in a string.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.boxed()
.collect(Collectors.toMap(list1::get, list2::get));
Explanation:
This problem demonstrates how to use Java Streams for: zip two lists into a map using index.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.findFirst();
Explanation:
This problem demonstrates how to use Java Streams for: find the first recurring element in a list.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
37. Extract domain names from a list of emails
.distinct()
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: extract domain names from a list of emails.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.reduce((a, b) -> {
int i = 0;
while (i < a.length() && i < b.length() && a.charAt(i) == b.charAt(i)) i++;
}).orElse("");
Explanation:
This problem demonstrates how to use Java Streams for: find longest common prefix of a list of strings.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.average().orElse(0);
Explanation:
This problem demonstrates how to use Java Streams for: calculate the variance of a list of integers.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.sorted(Comparator.comparingInt(String::length).reversed())
.limit(3)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: find the top 3 longest words in a paragraph.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Explanation:
This problem demonstrates how to use Java Streams for: detect circular references in a list of parent-child
relationships.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
sorted.get(list.size()/2);
Explanation:
This problem demonstrates how to use Java Streams for: calculate median of a list using streams.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
Explanation:
This problem demonstrates how to use Java Streams for: create a map of initials to concatenated names.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
44. Find the second most frequent element
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.skip(1).findFirst().map(Map.Entry::getKey).orElse(null);
Explanation:
This problem demonstrates how to use Java Streams for: find the second most frequent element.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(StringBuilder::new, StringBuilder::appendCodePoint,
StringBuilder::append).toString()));
Explanation:
This problem demonstrates how to use Java Streams for: identify anagrams in a list of strings.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Explanation:
This problem demonstrates how to use Java Streams for: create a suffix tree-like structure from list of strings.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: get all possible pairs of two lists using flatmap.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.collect(Collectors.groupingBy(e -> {
}));
Explanation:
This problem demonstrates how to use Java Streams for: group employees by age range buckets (20-29,
30-39...).
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.distinct()
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: extract hashtags from a list of tweets.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.
.mapToObj(list::get)
.collect(Collectors.toList());
Explanation:
This problem demonstrates how to use Java Streams for: remove consecutive duplicates from a list using
streams.
The key Stream operations involved include mapping, filtering, grouping, reducing, or flatMapping based on
This is useful in real-world applications like analytics, processing, or building backend logic for services.