Java Stream sorted() with examples

 182 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream sorted() with examples.

sorted() allows us to sort the elements in natural order.

sorted() is a part of the Stream pipeline and an intermediate operation. It belongs to the Stream interface.

Method description

Stream<T> sorted()
Stream<T> sorted(Comparator<? super T> comparator)

As per official documentation,

sorted() – Returns a stream consisting of the elements of this stream, sorted according to the natural order. If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

sorted(comparator) – Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

Since sorted() is an intermediate operation it will have two characteristics.

  1. sorted() is lazy loading operation. This means data will only be sorted when a terminal operation is invoked on the stream.
  2. sorted() operation on a stream will always create a new stream output.

Let us now see a few examples of Java 8 stream sorted()

Java stream sorted() operation example

In this example, a list of strings is sorted in natural order i.e ascending order.

import java.util.ArrayList;
import java.util.List;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("z");
		list.add("b");
		list.add("q");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
		list.add("h");

		list.stream().sorted().forEach(System.out::println);

	}
}

Output

b
d
e
f
g
h
q
z

Java stream sorted(comparator) operation example – Descending order

In this example, a list of strings is sorted in descending order by providing a comparator interface.

import java.util.ArrayList;
import java.util.List;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("z");
		list.add("b");
		list.add("q");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
		list.add("h");

		list.stream().sorted((i, j) -> j.compareTo(i))
                             .forEach(System.out::println);

	}
}

Output

z
q
h
g
f
e
d
b

Java stream sorted(comparator) operation example – Ascending order

In this example, a list of strings is sorted in ascending order by providing a comparator interface.

import java.util.ArrayList;
import java.util.List;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("z");
		list.add("b");
		list.add("q");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
		list.add("h");

		list.stream()
                       .sorted(StaticReference::sortinAsc)
                       .forEach(System.out::println);

	}

	static int sortinAsc(String i, String j) {
		return i.compareTo(j);
	}
}

Output

b
d
e
f
g
h
q
z

Conclusion

Java Stream sorted() operation is mainly used for sorting the elements of Stream. sorted() will always create a new stream and it is lazily evaluated.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

3 thoughts on “Java Stream sorted() with examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.