A Stream sorted() method used to sort stream elements in to the specified order. If order not specified, it sorts elements in ascending order. It takes Consumer object that can be a method reference or a lambda expression. It is an intermediate operation that returns a Stream object that includes sorted elements of Stream.
1 Stream<T> sorted()
2 Stream<T> sorted(Comparator<? super T> comparator);
T : It represents a type of Stream elements that will be sorted.
comparator : It is an instance of comparator, if order of sorted() method needs to be change. If not specified, the default order will be ascending order.
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 public class Example {
7 public static void main(String[] args) {
8
9 List<String> strings = Arrays.asList("String", "Java", "Watch");
10 strings.stream().sorted()
11 .forEach(System.out::println);
12 }
13 }
In the above example, a list object is stream using stream() method. A sorted() method sort each Stream elements into ascending order and returns a Stream object that includes sorted strings. A Stream object with sorted strings will be print using Stream forEach() method.
Stream sorted() method with comparator
Stream sorted() method with comparator
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.Comparator;
5 import java.util.List;
6
7 public class Example {
8 public static void main(String[] args) {
9
10 List<String> strings = Arrays.asList("String", "Java", "Watch");
11
12 strings.stream().sorted(Comparator.reverseOrder())
13 .forEach(System.out::println);
14 }
15 }
In the above example, a Stream sorted() method called by passing a Comparator object. A Comparator reverseOrder() method returns an instance of Comparator object. It sort Stream elements in reverse order and returns Stream object. A Stream elements will be print using forEach() method.
Stream sorted() method with numbers
Stream sorted() method with numbers
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 public class Example {
7 public static void main(String[] args) {
8
9 List<Integer> strings = Arrays.asList(20, 10, 50, 40);
10
11 strings.stream().sorted()
12 .forEach(System.out::println);
13 }
14 }
In the above example, a list object is define with integer values. A Stream sorted() function is called that sort Stream elements in ascending order and returns a Stream object that includes sorted intgers. A sorted Stream object element will be printed using forEach() method,
Stream sorted() with custom comparator
Stream sorted() with custom comparator
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 public class Example {
7 public static void main(String[] args) {
8
9 List<Integer> strings = Arrays.asList(20, 10, 50, 40);
10
11 strings.stream().sorted((n1, n2) -> n2 - n1)
12 .forEach(System.out::println);
13 }
14 }
In the above example, a sorted() method sort each element of Stream based on custom Comparator and returns Stream object that include descending order numbers. A custom comparator sort elements in descending order. A Stream object elements printed using forEach() method.
Stream sorted() with Custom object
Stream sorted() with Custom object
1 package com.java;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 class Model {
7 private String name;
8 private Integer price;
9
10 public Model(String name, Integer price) {
11 this.name = name;
12 this.price = price;
13 }
14
15 public String getName() {
16 return this.name;
17 }
18
19 public Integer getPrice() {
20 return this.price;
21 }
22
23 public String toString() {
24 return this.name + " = " + this.price;
25 }
26 }
27 public class Example {
28 public static void main(String[] args) {
29
30 List<Model> models = new ArrayList<>();
31 models.add(new Model("Java", 1000));
32 models.add(new Model("Javascript", 1200));
33
34 models.stream().sorted((n1, n2) -> n2.getPrice() - n1.getPrice())
35 .forEach(System.out::println);
36 }
37 }
In the above example, a custom class Model is created that has two properties name and price. An list object is created with two Model object. A sorted() method sort Stream elements in descending order based on price property. It returns a Stream that contain sorted Model object in descending order. A resulting Stream object elements printed using forEach() method.
1 Javascript = 1200
2 Java = 1000
Related options for your search