Java Reference
In-Depth Information
Section 17.6.7 Summing and Averaging Employee Salaries
Stream method mapToDouble (p. 756) maps objects to double values and returns a DoubleStream .
The method receives an object that implements the functional interface ToDoubleFunction
(package java.util.function ). This interface's applyAsDouble method invokes an instance
method on an object and returns a double value.
Section 17.7 Creating a Stream<String> from a File
Files method lines creates a Stream<String> for reading the lines of text from a file.
Stream method flatMap (p. 760) receives a Function that maps an object into a stream—e.g., a
line of text into words.
Pattern method splitAsStream (p. 760) uses a regular expression to tokenize a String .
Collectors method groupingBy with three arguments receives a classifier, a Map factory and a
downstream Collector . The classifier is a Function that returns objects which are used as keys in
the resulting Map . The Map factory is an object that implements interface Supplier and returns a new
Map collection. The downstream Collector determines how to collect each group's elements.
Map method entrySet returns a Set of Map.Entry objects containing the Map 's key-value pairs.
Set method stream returns a stream for processing the Set 's elements.
Section 17.8 Generating Streams of Random Values
• Class SecureRandom 's methods ints (p. 762), longs and doubles (inherited from class Random )
return IntStream , LongStream and DoubleStream , respectively, for streams of random numbers.
•Method ints with no arguments creates an IntStream for an infinite stream (p. 762) of random
int values. An infinite stream is a stream with an unknown number of elements—you use a
short-circuiting terminal operation to complete processing on an infinite stream.
•Method ints with a long argument creates an IntStream with the specified number of random
int values.
•Method ints with two int arguments creates an IntStream for an infinite stream of random int
values in the range starting with the first argument and up to, but not including, the second.
•Method ints with a long and two int arguments creates an IntStream with the specified number
of random int values in the range starting with the first argument and up to, but not including,
the second.
•To convert an IntStream to a Stream<Integer> call IntStream method boxed .
Function static method identity creates a Function that simply returns its argument.
Section 17.9 Lambda Event Handlers
• Some event-listener interfaces are functional interfaces (p. 763). For such interfaces, you can im-
plement event handlers with lambdas. For a simple event handler, a lambda significantly reduces
the amount of code you need to write.
Section 17.10 Additional Notes on Java SE 8 Interfaces
• Functional interfaces must contain only one abstract method, but may also contain default
methods and static methods that are fully implemented in the interface declarations.
• When a class implements an interface with default methods and does not override them, the
class inherits the default methods' implementations. An interface's designer can now evolve an
interface by adding new default and static methods without breaking existing code that im-
plements the interface.
Search WWH ::




Custom Search