Stream sorted(Comparator comparator) example
Description
Stream sorted(Comparator<? super T> comparator)
returns a stream consisting of the elements of this stream,
sorted according to the provided Comparator.
Syntax
sorted
has the following syntax.
Stream<T> sorted(Comparator<? super T> comparator)
Example
The following example shows how to use sorted
.
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
//from w w w . java 2 s . c o m
public class Main {
public static void main(String[] args) {
List<String> stringList = Arrays.asList("1","1","2","3","4");
stringList.stream()
.sorted(Comparator.reverseOrder())
.forEach(System.out::println);
}
}
The code above generates the following result.