LongStream sorted() method in Java

Java 8Object Oriented ProgrammingProgramming

The sorted() method of the LongStream class in Java returns a stream with the elements of this stream in sorted order.

The syntax is as follows −

LongStream sorted()

To use the LongStream class in Java, import the following package −

import java.util.stream.LongStream;

Create a LongStream and add some elements −

LongStream longStream = LongStream.of(80L, 35L, 15L, 70L, 30L, 45L);

Now, sort the elements of the stream −

longStream.sorted()

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

Example

 Live Demo

import java.util.stream.LongStream;

public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(80L, 35L, 15L, 70L, 30L, 45L);
      System.out.println("Sorted elements in the stream = ");
      longStream.sorted().forEach(System.out::println);
   }
}

Output

Sorted elements in the stream =
15
30
35
45
70
80
raja
Updated on 30-Jul-2019 22:30:25

Advertisements