IntStream sorted() method in Java

Java 8ProgrammingObject Oriented Programming

The sorted() method in Java IntStream class is used to return a stream consisting of the elements of this stream in sorted order.

The syntax is as follows

IntStream sorted()

The sorted() method returns the new stream. To work with the IntStream class, you need to import the following package

import java.util.stream.IntStream;

Create an IntStream and add some elements

IntStream intStream = IntStream.of(30, 50, 70, 120, 150, 200, 250, 300);

Now, to sort the above stream elements, use the sorted() method

intStream.sorted()

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

Example

 Live Demo

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(30, 50, 70, 120, 150, 200, 250, 300);
      intStream.sorted().forEach(System.out::println);
   }
}

Output

30
50
70
120
150
200
250
300
raja
Published on 26-Mar-2019 14:58:36
Advertisements