DoubleStream sorted() method in Java


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

Updated on: 30-Jul-2019

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements