DoubleStream sorted() method in Java

Java 8Object Oriented ProgrammingProgramming

The sorted() method of the DoubleStream class returns a stream consisting of the elements of this stream in sorted order.

The syntax is as follows

DoubleStream sorted()

To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

Create a DoubleStream and add some elements to the stream

DoubleStream doubleStream = DoubleStream.of(78.9, 90.4, 27.9, 20.6, 45.3, 18.5);

Now, sort the elements of the stream

doubleStream.sorted().

The following is an example to implement DoubleStream sorted() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(78.9, 90.4, 27.9, 20.6, 45.3, 18.5);
      System.out.println("Sorted stream...");
      doubleStream.sorted().forEach(System.out::println);
   }
}

Output

Sorted stream...
18.5
20.6
27.9
45.3
78.9
90.4
raja
Published on 28-Mar-2019 13:10:48
Advertisements