java 8 stream sorted method example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations.
The Stream.sorted() method returns a sorted Stream.

Java 8 stream sorted method example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.w3schools;
import java.util.stream.Stream;
public class Test{
public static void main(String[] args) {
Stream.of("Jai", "Mahesh", "Vishal", "Naren", "Hemant")
.sorted()
.forEach(System.out::println);
}
}
package com.w3schools; import java.util.stream.Stream; public class Test{ public static void main(String[] args) { Stream.of("Jai", "Mahesh", "Vishal", "Naren", "Hemant") .sorted() .forEach(System.out::println); } }
package com.w3schools;

import java.util.stream.Stream;

public class Test{  
    public static void main(String[] args) {  
    	Stream.of("Jai", "Mahesh", "Vishal", "Naren", "Hemant")
    	.sorted()
        .forEach(System.out::println);
    }  
}  

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hemant
Jai
Mahesh
Naren
Vishal
Hemant Jai Mahesh Naren Vishal
Hemant
Jai
Mahesh
Naren
Vishal