DoubleStream example

In Java 8, java.util.stream.DoubleStream was introduced to deal with primitive doubles which brings a new way to solving old problems of finding max or largest value in array, find min or smallest value in array, sum all elements in array, and average of all values in array. If you are looking to apply max, min, sum and average reduce techniques the stream reduction example or DoubleSummaryStatistics, a specialized object for collecting statistics, show how to use them.

In these examples we will show common methods exposed by DoubleStream that deal with double-value elements. In addition to dealing with doubles, IntStreams is a specialized class to deal with primitive ints and LongStream for dealing with primitive type long.

builder

There are various ways to build a stream and DoubleStream has a builder that allows you to chain together calls to set up configuration followed by a build method that creates the stream. This code snippet below makes a call to the builder and then adds two primitive double values. It then calls the sum method to find the sum of the stream.

@Test
public void doublestream_builder() {

    double sum = DoubleStream.builder().add(10).add(10).build().sum();

    assertEquals(20, sum, 0);
}

concat

In the event where you have seperate streams, DoubleStream provides the ability to concat or combine streams together. To demonstrate this we created two streams via the builder, concatenated and then called the sum method.

@Test
public void doublestream_concat() {

    DoubleStream first = DoubleStream.builder().add(10).build();
    DoubleStream second = DoubleStream.builder().add(10).build();

    DoubleStream third = DoubleStream.concat(first, second);

    assertEquals(20, third.sum(), 0);
}

empty

The DoubleStream.empty returns an empty sequential DoubleStream.

@Test
public void doublestream_empty() {

    DoubleStream emptyStream = DoubleStream.empty();

    assertEquals(0, emptyStream.count());
}

generate

The DoubleStream.generate allows you to produce an infinite stream of values on demand. The code below we will generate 10 ints and then find the distinct elements which will return the numeric value of 1.

@Test
public void doublestream_generate() {

    OptionalDouble one = DoubleStream.generate(() -> 1).limit(10)
            .distinct().findFirst();

    assertEquals(1, one.getAsDouble(), 0);
}

iterate

Similar to the DoubleStream.generate, the DoubleStream.iterate will return an infinite sequential ordered DoubleStream. The first parameter to DoubleStream.iterate is the seed and in our case 0. It will iterate from 0 and for every element will add 3, so 0 + 3, 3 + 3 and so on. Since we apply the limit it will return 3 iterations thus returning 0, 3, 6.

@Test
public void doublestream_iterate() {

    List<Double> numbers = DoubleStream.iterate(0, n -> n + 3).limit(3)
            .boxed().collect(Collectors.toList());

    assertThat(numbers,
            contains(new Double(0), new Double(3), new Double(6)));
}

of

The DoubleStream.of method returns a stream whose elements are specified. Below a stream will be initialized with 5,10 and then max taken which will return 10 as the unit test reads.

@Test
public void doublestream_of() {

    OptionalDouble max = DoubleStream.of(5, 10).max();

    assertEquals(10, max.getAsDouble(), 0);
}

Map to DoubleStream

This example will take an ArrayList of strings and then map them to an DoubleStream by passing Double.parseDouble. Then for testing purposes we will call DoubleStream.max which will return 3.

@Test
public void map_to_doublestream() {

    List<String> doubles = new ArrayList<String>();
    doubles.add("1");
    doubles.add("2");
    doubles.add("3");

    OptionalDouble doubleStream = doubles.stream()
            .mapToDouble(Double::parseDouble).max();

    assertEquals(3, doubleStream.getAsDouble(), 0);
}

Convert to stream of objects

If you are looking to convert DoubleStream to a stream of Double, you can call boxed method which returns the elements in the stream each boxed to an Double.

@Test
public void convert_to_stream_of_objects() {

    double[] numbers = { 1, 2, 3, 4, 5, 6 };

    List<Double> listOfDoubles = Arrays.stream(numbers).boxed()
            .collect(Collectors.toList());

    assertThat(
            listOfDoubles,
            contains(new Double(1), new Double(2), new Double(3),
                    new Double(4), new Double(5), new Double(6)));

}

Specify default

OptionalDouble is a specialized version of Optional. In the instance where you want to provide a default other than 0.0, you can call the OptionalDouble.orElse. In this example, we want to find the max value of an empty list. Since there isn't any values present it will return 5.

@Test
public void provide_default() {

    List<String> doubles = new ArrayList<String>();

    double optionalDouble = doubles.stream()
            .mapToDouble(Double::parseDouble).max().orElse(5);

    assertEquals(5, optionalDouble, 0);
}