Creation and basics

  1. Print the sum of squares of all even numbers from 1 to 100 using IntStream.rangeClosed. Then list all primes up to 100 using noneMatch, and convert between IntStream, Stream<Integer> and int[] using boxed, mapToInt and toArray.
  2. Create streams from an array, a collection, Stream.of, a string's chars(), Stream.generate with limit, and the three-argument Stream.iterate with a predicate. Print each one, and note in comments which are infinite.
  3. Write a page(List<String> data, int pageNo, int pageSize) method using skip and limit. Before paging, clean the data with filter, map(String::trim), distinct and sorted. Handle out-of-range pages.
  4. Given sorted temperature readings, use takeWhile to get readings until the first one above 40, and dropWhile to get everything after it. Explain in a comment why these differ from filter on sorted data.

Collectors

  1. Group employees by department, then by age bracket (below 30, 30-45, above 45) using nested groupingBy. Count each bracket and print the result as a tidy tree.
  2. Convert a list of products to a Map<String, Double> with toMap. First show the IllegalStateException on duplicate keys, then fix it with a merge function that keeps the higher price. Use a LinkedHashMap supplier to keep insertion order.
  3. Use partitioningBy to split students into pass and fail, with a downstream averagingDouble for marks. Add summarizingInt for age statistics, and use teeing (Java 12+) to compute the min and max marks in a single pass.
  4. Write a custom Collector using Collector.of that joins non-null strings with a delimiter, prefix and suffix, and caps the output at N items with ... appended. Test it on a sequential and a parallel stream.
  5. For a list of employees with a Set<String> skills field, build Map<Department, Set<String>> of all skills per department using flatMapping. Add filtering to include only employees earning above a threshold, and collectingAndThen to return an unmodifiable map.

Transformations

  1. Given Order objects, each containing a list of LineItem (product, qty, price), use flatMap to compute total revenue, the distinct products sold, and the top 3 products by quantity.
  2. Use flatMap to generate the Cartesian product of two lists, all Pythagorean triples with each side up to 50, and to flatten a Map<String, List<String>> into "key:value" strings.
  3. Write string-processing utilities using streams only: first non-repeating character (using LinkedHashMap and groupingBy), vowel and consonant counts, title-casing every word, and reversing the word order of a sentence.
  4. Given an int[][] matrix, use streams to compute row sums, column sums, the transpose, the global maximum, and a flattened sorted array without duplicates.

Reduce, Optional and laziness

  1. Compute a factorial of 25 with BigInteger using reduce. Show all three reduce forms, including the combiner version used with parallel streams. Then write Optional<String> emailDomain(Optional<User>) using map, flatMap and filter, with no get() and no null checks.
  2. Demonstrate laziness. Chain filter, map and limit with peek in between, and print the order in which elements flow through. Show how findFirst and anyMatch short-circuit, and how sorted breaks the one-by-one flow.
  3. Show the dangers of parallel streams: add to a shared ArrayList from a parallel forEach and observe the lost or corrupt results. Fix it with collect. Also compare forEach against forEachOrdered on a parallel stream, and run the work in a custom ForkJoinPool, printing the thread names.