Streams – DoubleStream class

DoubleStream class in Java represents a sequence of double-valued elements. It is introduced as part of Java 8 Numeric Streams addition, and it is located inside the java.util.stream package.

 DoubleStream is a double primitive specialization of a Stream.

Working with DoubleStream is very similar to working with IntStream and LongStream classes. Methods and operations are almost the same.

How to create a DoubleStream in Java?

There are several ways of creating a DoubleStream:

1. DoubleStream.of() method

Using the of() method, we can specify the values ​​we want the DoubleStream to contain.
There are 2 types of the of() function, one which takes only one element and the one that can take more than one.

Syntax:

DoubleStream.of(2.4);
DoubleStream.of(7.1, 10.5, 12.7);


Here, the first statement will create a DoubleStream that will hold only one element (2.4).
The second statement creates a DoubleStream that holds values 7.1, 10.5, 12.7.

Note: Unlike an IntStream or a LongStream, a DoubleStream does not have a range() or a rangeClosed() method as there are infinite values between a specified range.


2. DoubleStream.iterate() method

We use the iterate() method to create infinite streams. However, we can limit it by using the limit() function.

Syntax:

DoubleStream.iterate(0, i -> i + 2).limit(10);


3.
LongStream.generate() method

Returns an infinite sequential unordered stream where each element is generated by the provided DoubleSupplier. This is suitable for generating constant streams, streams of random elements, etc.

Syntax:

DoubleStream.generate(() -> Math.random() * 5).limit(10);


The above code will return a DoubleStream of 10 random decimal numbers multiplied by 5.


Examples

Example 1:
Iterate over a DoubleStream.
It supports the forEach() method for iterating over the elements, just like the regular Stream.

class Test {

  public static void main(String[] args) {
    DoubleStream doubleStream = DoubleStream.of(1.2, 3.1, 7.2, 5.3, 9.8);
    doubleStream.forEach(item -> System.out.print(item + " "));
  }
}

Output: 1.2 3.1 7.2 5.3 9.8


Example 2:
Convert a DoubleStream to a List.
Since Collections in Java can not store the primitive values directly, we need to use the boxed() method of DoubleStream class to convert the primitive type to the Wrapper class.

class Test {

  public static void main(String[] args) {
    List<Double> decimals = DoubleStream.of(1.4, 2.5, 3.1, 4.7, 5.1)
            .boxed()
            .collect(Collectors.toList());

    System.out.println(decimals); 
  }
}

Output: [1.4, 2.5, 3.1, 4.7, 5.1]


Example 3:
Convert a DoubleStream to an array using the toArray() function:

class Test {

  public static void main(String[] args) {
    double[] array = DoubleStream.of(1.3, 2.1, 3.9, 4.1, 5.2).toArray();
    Arrays.stream(array).forEach(item -> System.out.print(item + " "));
  }
}

Output: 1.3 2.1 3.9 4.1 5.2


Example 4:
Filter a DoubleStream using the filter() function:

class Test {

  public static void main(String[] args) {
    DoubleStream.of(3, 4, 5, 6, 7, 8)
            .filter(i -> i % 2 == 0)
            .forEach(item -> System.out.print(item + " "));
  }
}

Output: 4.0 6.0 8.0


That’s it!

 

Leave a Reply

Your email address will not be published.