Streams

The Stream Concept

Some Facts About Streams

Lazy Processing

Your Turn

Which of the following counts whether the given stream has at least ten strings with three letters?

boolean atLeastTen = stream
   .filter(w -> w.length() == 3)
   .limit(10)
   .count() == 10;

boolean atLeastTen = stream
   .filter(w -> w.length() == 3)
   .count() >= 10;
  1. Only the first
  2. Only the second
  3. Both, but the first is more efficient
  4. Both, but the second is more efficient

Producing Streams

Producing Streams 2

  • You can turn any stream into a parallel stream
  • Your Turn

    In this program, complete the methods that yield

    Collecting Results 1

    Collecting Results 2

    Your Turn

    Complete the method in this program that collects the first n strings of length ≤ maxLength from the collection c in a Set<String> (which might have fewer than n elements after discarding duplicates). Use streams.

    Tip: One Stream Operation Per Line

    Stream Tranformations: map

    Your Turn

    Which of the following yields a stream of all words that start with a or A, converted to lowercase?

    Stream<String> result = words
       .filter(w -> w.substring(0, 1).equalsIgnoreCase("a"))
       .map(w -> w.toLowerCase());
    

    Stream<String> result = words
       .map(String::toLowerCase)
       .filter(w -> w.substring(0, 1).equals("a"));
    
    1. Only the first
    2. Only the second
    3. Both, but the first is more efficient
    4. Both, but the second is more efficient

    More Stream Transformations

    Common Error: Don't Use a Terminated Stream

  • If you want to do what the example tried to do, you need two streams.
    List<String> result1 = list.stream()
       .limit(50)
       .collect(Collectors.toList()); // This stream can no longer be used
    List<String> result2 = list.stream() // Create another stream
       .skip(50)
       .limit(50)
       .collect(Collectors.toList());
  • Exercises

    1. We want to count the distinct vowels in a word. This is easy with streams. Turn a word into a stream of single-letter strings (as before, with s.split("")). Filter out the vowels. (Use "aeiou".contains(c)...) and count the distinct ones.
      But what about accents? Strip them off, using this code:
      Normalizer.normalize(c, Normalizer.Form.NFD).substring(0, 1)
      The “normalization form D” (decomposition) takes an accented character such as ô and decomposes it into an o and the accent ^
      Complete this program.
    2. Now read in all words from a given file and find how many of them have five distinct vowels. Then list the twenty shortest ones. Complete this program.
      You have the static Words.distinctVowels method at your disposal. (It is hidden so as not to give away the answer to the previous exercise.)
    3. (Hard) For each word in a stream, determine the “vowelness”, i.e. the number of vowels - the number of consonants. Produce the n words with the highest vowelness paired with the vowelness value. Sort first by vowelness, then by the string. Complete this program.
      This time, you have a hidden static method long Words.vowels(String w) at your disposal that yields the number of vowels in w, including duplicates.