How it works...

This recipe discusses three groups of methods under the java.util.stream.Stream class, and these are the stream creation methods, intermediate operations, and terminal stream methods.

Streams can be generated in many ways, but the most straightforward technique is to use the static method of() when creating a stream from individual objects or from an existing array, and the collection's instance stream() method when creating a sequential stream from a List, Set, or Map of mutable containers. The Stream class has a static method, empty(), which can create a zero-size stream, while another static, generate(), produces an infinite stream of objects.

The second batch of operations is the set of methods that fills up the pipeline of streams that are executed only once the compiler executes the last stream operation. These Stream methods are summarized as follows:

  • filter(): Removes some of the stream objects based on criteria or conditions implemented in the Predicate functional interface
  • sorted(): Sorts the stream objects based on the comparator's rule for comparison, written in lambda expression
  • map(), flatMap(), mapToInt(), mapToDouble(), mapToLong(): Maps each original Stream object to a new object type, based on some variations of Function implemented to extract a certain result
  • limit(): Cuts the size of the stream to be retrieved based on a certain Predicate
  • distinct(): Retrieves a unique list of stream objects based on Predicate

The preceding intermediate methods only update, manipulate, extract, define, streamline, and remove data without changing the original data structure.

The last batches of stream methods are the so-called terminal operations which are the last operations to appear in the pipeline of streams. Their returned value is either mutable containers (for example, collections or arrays) or void. There are five known methods under this group, namely count(), match(), findFirst(), forEach(), collect() with Collectors, and reduce(). The forEach() method internally iterates all Stream objects and can output all of them using OutputStream. The reduce() method asks a formula to perform transactions on stream objects in order to generate a single-valued result. On the other hand, the return type of collect() operations depends on the static method of the Collectors class. It can return a mutable container when Collectors.toList(), Collectors.toSet(), Collectors.toMap(), or Collectors.toCollection() is called. Or, it can return a single-value element if the method calls Collectors.averagingInt(), Collectors.counting(), or Collectors.summingInt(), for instance. On the other hand, the match() method has so many variations, such as allMatch(), which returns true if all objects satisfy the Predicate, noneMatch(), that returns true if none of the objects satisfy the Predicate, and anyMatch(), which returns true if one stream object fulfills the Predicate input. Pretty straightforwardly, the method count() returns the number of stream objects, while findFirst() returns the first object given a particular constraint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.172.200