Java Reference
In-Depth Information
Section 17.4.1 Creating a Stream<Integer>
•Interface Stream (package java.util.stream; p. 744) is a generic interface for performing stream
operations on objects. The types of objects that are processed are determined by the Stream 's source.
• Class Arrays provides overloaded stream methods for creating IntStream s, LongStream s and
DoubleStream s from int , long and double arrays or from ranges of elements in the arrays.
Section 17.4.2 Sorting a Stream and Collecting the Results
Stream method sorted (p. 745) sorts a stream's elements into ascending order by default.
• To create a collection containing a stream pipeline's results, you can use Stream method collect
(a terminal operation). As the stream pipeline is processed, method collect performs a mutable
reduction operation that places the results into an object, such as a List , Map or Set .
•Method collect with one argument receives an object that implements interface Collector
(package java.util.stream ), which specifies how to perform the mutable reduction.
• Class Collectors (package java.util.stream ) provides static methods that return predefined
Collector implementations.
Collectors method toList transforms a Stream<T> into a List<T> collection.
Section 17.4.3 Filtering a Stream and Storing the Results for Later Use
Stream method filter (p. 745) receives a Predicate and results in a stream of objects that match
the Predicate . Predicate method test returns a boolean indicating whether the argument sat-
isfies a condition. Interface Predicate also has methods and , negate and or .
Section 17.4.5 Sorting Previously Collected Results
• Once you place the results of a stream pipeline into a collection, you can create a new stream
from the collection for performing additional stream operations on the prior results.
Section 17.5.1 Mapping String s to Uppercase Using a Method Reference
Stream method map (p. 747) maps each element to a new value and produces a new stream with
the same number of elements as the original stream.
• A method reference (p. 747) is a shorthand notation for a lambda expression.
ClassName :: instanceMethodName represents a method reference for an instance method of a
class. Creates a one-parameter lambda that invokes the instance method on the lambda's argu-
ment and returns the method's result.
objectName :: instanceMethodName represents a method reference for an instance method that
should be called on a specific object. Creates a one-parameter lambda that invokes the instance
method on the specified object—passing the lambda's argument to the instance method—and
returns the method's result.
ClassName :: staticMethodName represents a method reference for a static method of a class.
Creates a one-parameter lambda in which the lambda's argument is passed to the specified a
static method and the lambda returns the method's result.
ClassName :: new represents a constructor reference. Creates a lambda that invokes the no-argu-
ment constructor of the specified class to create and initialize a new object of that class.
Section 17.5.2 Filtering String s Then Sorting Them in Case-Insensitive Ascending
Order
Stream method sorted (p. 748) can receive a Comparator as an argument to specify how to com-
pare stream elements for sorting.
• By default, method sorted uses the natural order for the stream's element type.
Search WWH ::




Custom Search