Showing posts with label Streams. Show all posts
Showing posts with label Streams. Show all posts

Friday, September 29, 2017

Java 9 Streams : dropWhile()

In Java 9, comes with a few good additions to the Stream API. For more information on the Java 8 Streams, go to the Java 8 Page. The following new methods were added to the Java 9 Stream API.
  1. dropWhile
  2. dropWhile
  3. iterate
  4. ofNullable
In this post we will take a look at the dropWhile() method.

Thursday, September 28, 2017

Java 9 Streams: iterate() and ofNullable() methods

In Java 9, comes with a few good additions to the Stream API. For more information on the Java 8 Streams, go to the Java 8 Page. The following new methods were added to the Java 9 Stream API.
  1. dropWhile
  2. dropWhile
  3. iterate
  4. ofNullable
In this post we will take a look at the iterate() and ofNullable() methods.

Wednesday, September 27, 2017

Java 9 Streams : takeWhile() method

In Java 9, comes with a few good additions to the Stream API. For more information on the Java 8 Streams, go to the Java 8 Page. The following new methods were added to the Java 9 Stream API.
  1. takeWhile
  2. dropWhile
  3. iterate
  4. ofNullable
In this post we will take a look at the takeWhile() method.

Sunday, September 24, 2017

Using Comparators with Java 8 Streams

This post gives a few examples of how to use Comparator with Java 8 streams to Sort and to find the Maximum and Minimum elements in a stream.

Comparing Simple types with Comparator.comparing* methods

The Comparator.comparingDouble, Comparator.comparingInt and Comparator.comparingLong etc. methods can be used to do sorting or finding max and min from stream. The following example uses the comparingDouble method to compare and sort a stream of doubles generated using the Stream.generate() method. The Stream.generate has been explained my earlier post "Java 8 Streams"
  // Sort a stream of Doubles
  Stream.generate(Math::random).limit(10).sorted(Comparator.comparingDouble(Double::valueOf)).forEach(System.out::println);

Thursday, August 31, 2017

Java 8 Stream collect()

In Java 8 Streams, the collect() method is a terminal operation, which can be used collect elements of a stream into a list or group them into a Map etc. collect method takes java.util.stream.Collector as parameter. While you can create your own collector by implementing the java.util.stream.Collector interface, the more common way to instantiate a Collector is through the use of Collectors class. The following examples demonstrate a few ways to use the collect() method with different types of Collectors.

Wednesday, August 30, 2017

Java 8 Streams flatMap()

In Java 8 Stream API, flatMap() method is an intermediate functions for streams, in that, it will produce a final result but used to apply transformations on the elements of a Stream. It is possible to to chain multiple flatMap() while processing a single stream. Both map() and flatMap() both can be applied to a Stream<T> and return a Stream<R> or transformed elements, but the main difference is that map() produces one output element for each input element, whereas flatMap() produces any number (0-n) of output elements for each input element. Put simply, when using a map(), the resulting stream would have same number or elements as the input stream, where as it can be different in case of a flatMap(). The following example illustrates the differences the use of flatMap()

Monday, August 28, 2017

Java 8 Streams map()

In Java 8 Streams, map() is an intermediate function that can be used to apply a function to every element of a Stream. A few variations of map() include mapToInt(), mapToLong etc. which return streams of the corresponding types. The following examples show a few ways to use the map() function on streams.

Sunday, August 27, 2017

Java 8 Streams : map vs flatMap

In Java 8 Stream API, map and flatMap methods are provided as part of the java.util.stream.Stream Interface. Both methods are intermediate functions for streams, in that, they do not produce a final result but are used to apply transformations on the elements of a Stream. It is possible to to chain multiple map() and flatMap() while processing a single stream.

Saturday, August 26, 2017

Java 8 Streams

Java 8 Streams API provide a declarative way to process sequence of data elements. A stream can be a sequence of elements from a collection, a I/O channel or a generator function. To process a stream, a set of operations are composed into a pipeline. A stream pipeline consists of a Source (data stream), one or more intermediate operations (apply transformations on the stream) and a terminal operation that produces a result or a side-effect. The following example (from the Stream javadoc), has one source (widgets), two intermediate operations (filter and mapToInt) and a terminal operation (sum).
  int sum = widgets.stream() // Source
                    .filter(w -> w.getColor() == RED) // Intermediate operation
                    .mapToInt(w -> w.getWeight())  //Intermediate operation
                    .sum();  // Terminal operation, produces result
In this article, we will go over the basics of streams and how to create or initialize the source for a stream pipeline.

Popular Posts