UP | HOME
2015-11-09 | ../../ | Slides

Streams of Java 8

1 Abstract

Streams of Java8. Prereq: Java 8 Lambda Expressions, functional interfaces and method references.

2 Overview

  1. The streams discussed here are unrelated to InputStream and OutputStream from Java IO.
  2. A stream is a sequence of "items". Can be infinite.
  3. Never expect to have the "entire" stream to be available "at once". Typically, we use items from the head, then wish to access the next item. This is computed lazily (i.e., on demand).
  4. Java8 stream s are monads. "In functional programming, a monad is a structure that represents computations defined as sequences of steps" – WikiPedia.
  5. Any collection can be made into a stream. A List <Integer> ilst = Arrays.asList (1, 2, 3, 5, 7, 11) can be made into a stream of those 6 integers.

2.1 Example: An Int Stream

IntStream ns = ss.stream().mapToInt(s -> s.length()).filter(i -> i%2 != 0);
  1. ss was a List<String>. The stream() converted it to a stream. mapToInt mapped each string to its length. The filter function removed all odd numbers from the result. There were two uses of the lambda expressions. (Past tense was used just to explain.)
  2. The object named ns will-contain/ contains the resulting stream.
  3. But, the entire stream value is not constructed "up front". It is computed on a need-to-have basis, known as lazy evaluation. The above is "just" a declaration of an object that you "want."

2.2 Ordered Streams

  1. A stream whose source is an array, a List, or a generator function, is ordered.
  2. A stream whose source is a Set is unordered.

3 Map + Reduce

  1. We already used the idea of a map: Apply a function to each value of a collection and gather the resulting collection of the same structure.
  2. Reduce is the name given to an operation that takes a collection and computes "one-thing" out of it.
  3. Example: Sum up all the numbers of a Set.

3.1 Example

Example below is from https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html

double average = roster
   .stream()
   .filter(p -> p.getGender() == Person.Sex.MALE)
   .mapToInt(Person::getAge)
   .average()
   .getAsDouble();
  1. p -> p.getGender() = = Person.Sex.MALE a lambda expression as as a true/false yielding predicate.
  2. mapToInt is a function that applies the function given as its argument to every item in the stream. A new stream is constructed with these results.
  3. Person::getAge a lambda expression omitting the bound var of the lambda
  4. average is a reduction.

4 Example: Log Analysis

  1. ../Java/src/AuthLogStreamEx.java Look for "Invalid" attempts to login from the /var/log/auth.log file; 50+ lines long. A complete Java program. Colorized | Plain
  2. % javac AuthLogStreamEx.java
  3. % java AuthLogStreamEx auth.log.txt invalidUsers.txt
  4. Input data file for the above: auth.log.txt Copy of a /var/log/auth.log.
  5. Output data file for the above: invalidUsers.txt

5 References

  1. http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html Reference.
  2. Brian Goetz, Lambdas and Streams in Java 8 Libraries, DrDobbs article, 2014. Recommended Reading
  3. Lectured using these PDF slides by www.cs.cmu.edu/~charlie. "Lambdas and Streams in Java8", 2014. Required Reading.

Copyright © 2015 www.wright.edu/~pmateti