Java Streams - IntStream sorted() example








IntStream sorted() returns a stream consisting of the elements of this stream in sorted order. This is a stateful intermediate operation.

Syntax

sorted has the following syntax.

IntStream sorted()

Example

The following example shows how to use sorted.

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 4);
    i.sorted()
     .forEach(System.out::println);
  }
}

The code above generates the following result.