Close

Java 8 Streams - IntStream.sorted Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.IntStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.IntStreamIntStreamLogicBig

Method:

IntStream sorted()

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

Examples


package com.logicbig.example.intstream;

import java.util.Arrays;
import java.util.stream.IntStream;

public class SortedExample {

public static void main(String... args) {
int[] ints = IntStream.generate(() -> (int) (Math.random() * 1000))
.limit(5)
.peek(System.out::println)
.sorted().toArray();
System.out.println(Arrays.toString(ints));
}
}

Output

401
312
627
463
409
[312, 401, 409, 463, 627]




See Also