IntStream sorted() in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report IntStream sorted() returns a stream consisting of the elements of this stream in sorted order. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements. Stateful intermediate operations may need to process the entire input before producing a result. For example, one cannot produce any results from sorting a stream until one has seen all elements of the stream. Syntax : IntStream sorted() Where, IntStream is a sequence of primitive int-valued elements. This is the int primitive specialization of Stream. Exception : If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed. Return Value : IntStream sorted() method returns the new stream. Example 1 : Using IntStream sorted() to sort the numbers in given IntStream. Java // Java code to sort IntStream // using IntStream.sorted() import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.of(10, 9, 8, 7, 6); // displaying the stream with sorted elements // using IntStream.sorted() function stream.sorted().forEach(System.out::println); } } Output: 6 7 8 9 10 Example 2 : Using IntStream sorted() to sort the random numbers generated by IntStream generator(). Java // Java code to sort IntStream // using IntStream.sorted() import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream by generating // random elements using IntStream.generate() IntStream stream = IntStream.generate(() -> (int)(Math.random() * 10000)) .limit(5); // displaying the stream with sorted elements // using IntStream.sorted() function stream.sorted().forEach(System.out::println); } } Output: 501 611 7991 8467 9672 Comment More infoAdvertise with us Next Article Sorting in Java S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream java-intstream +2 More Practice Tags : JavaMisc Similar Reads SortedMap Interface in Java SortedMap is an interface in the collection framework that is a part of java.util package and extends the Map interface. It represents a map that maintains its keys in a sorted order. The keys in a SortedMap are sorted according to their natural ordering or by a Comparator provided at the time of ma 10 min read Sorting in Java Whenever we do hear sorting algorithms come into play such as selection sort, bubble sort, insertion sort, radix sort, bucket sort, etc but if we look closer here we are not asked to use any kind of algorithms. It is as simple sorting with the help of linear and non-linear data structures present wi 5 min read Sorting in Java Whenever we do hear sorting algorithms come into play such as selection sort, bubble sort, insertion sort, radix sort, bucket sort, etc but if we look closer here we are not asked to use any kind of algorithms. It is as simple sorting with the help of linear and non-linear data structures present wi 5 min read Stream sorted() in Java Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously s 2 min read Arrays.sort() in Java The Arrays.sort() method is used for sorting the elements in an Array. It has two main variations: Sorting the entire array (it may be an integer or character array) Sorting a specific range by passing the starting and ending indices.Below is a simple example that uses the sort() method to arrange a 6 min read Java Program for Insertion Sort Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. In this article, we will write the program on Insertion Sort in Java.Please refer complete article on Insertion Sort for more details! Algorithm of Insertion SortThe algorithm of Insertion Sort is men 2 min read Like