In this post, we will learn “How to sort a stream of numbers, strings and list in Java 8?”.
Sorting of a Stream in Java 8...!!! Share on X
In this tutorial, we are going to cover below topics:
- Sorting of stream of number in Ascending(Natural) Order
- Sorting of stream of number in Descending(Reverse) Order
- Sorting of stream of string in Ascending Order
- Sorting of stream of string in Descending Order
- Sorting of stream of list in Ascending Order
- Sorting of stream of list custom object in Ascending Order
Let’s begin:
1. Sort number in natural order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Comparator; import java.util.stream.Stream; public class Number_Sort_Stream_Java8_Example1 { public static void main(String[] args) { System.out.println("Sorted stream of Numbers in Ascending Order Example"); Stream <Integer> numberStream = Stream.of(54, 22, 78, 11, 9); numberStream.sorted(Comparator.naturalOrder()) .forEach(System.out::println); } } |
1 2 3 4 5 6 | Sorted stream of Numbers in Ascending Order Example 9 11 22 54 78 |
2. Sort number in reverse order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Comparator; import java.util.stream.Stream; public class Number_Sort_Stream_Java8_Example2 { public static void main(String[] args) { System.out.println("Sorted stream of Numbers in Descending Order Example"); Stream <Integer> numberStream = Stream.of(54, 22, 78, 11, 9); numberStream.sorted(Comparator.reverseOrder()) .forEach(System.out::println); } } |
1 2 3 4 5 6 | Sorted stream of Numbers in Descending Order Example 78 54 22 11 9 |
3. Sort string in natural order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Comparator; import java.util.stream.Stream; public class String_Sort_Stream_Java8_Example1 { public static void main(String[] args) { System.out.println("Sorted stream of Strings in Ascending Order Example"); Stream <String> stringStream = Stream.of("Tom", "Mark", "Lucas", "Amber", "Cash", "Joey"); stringStream.sorted(Comparator.naturalOrder()) .forEach(System.out::println); } } |
1 2 3 4 5 6 7 | Sorted stream of Strings in Ascending Order Example Amber Cash Joey Lucas Mark Tom |
4. Sort string in reverse order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Comparator; import java.util.stream.Stream; public class String_Sort_Stream_Java8_Example2 { public static void main(String[] args) { System.out.println("Sorted stream of Strings in Descending Order Example"); Stream <String> stringStream = Stream.of("Tom", "Mark", "Lucas", "Amber", "Cash", "Joey"); stringStream.sorted(Comparator.reverseOrder()) .forEach(System.out::println); } } |
1 2 3 4 5 6 7 | Sorted stream of Strings in Descending Order Example Tom Mark Lucas Joey Cash Amber |
5. Sort list in natural order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Arrays; import java.util.Comparator; import java.util.List; public class List_Sort_Stream_Java8_Example1 { public static void main(String[] args) { List <String> listOfStudents = Arrays.asList("Tom", "Mark", "Lucas", "Amber", "Cash", "Joey"); System.out.println("Before Sorting:\n"); for (String s: listOfStudents) { System.out.println(s); } System.out.println("\nAfter Sorting:\n"); //Sort the list directly using Lambda expression listOfStudents.stream().sorted(Comparator.naturalOrder()) .forEach(System.out::println);; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Before Sorting: Tom Mark Lucas Amber Cash Joey After Sorting: Amber Cash Joey Lucas Mark Tom |
6. Sort custom list object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import java.util.ArrayList; import java.util.List; public class List_Sort_Stream_Java8_Example2 { public static void main(String[] args) { List <Actor> listOfActors = getActors(); System.out.println("Before Sorting:\n"); for (Actor actor: listOfActors) { System.out.println(actor.getAge() + "---" + actor.getName()); } System.out.println("\nAfter Sorting:\n"); //Sort the list directly using Lambda expression listOfActors.sort((Actor a1, Actor a2) -> a1.getAge() - a2.getAge()); //Print the sorted list using Java 8 foreach method listOfActors.forEach((actor) -> System.out.println(actor.getAge() + "---" + actor.getName())); } static List <Actor> getActors() { List <Actor> actors = new ArrayList <Actor> (); actors.add(new Actor(5, "yashu")); actors.add(new Actor(64, "gaurangee")); actors.add(new Actor(22, "pulkit")); actors.add(new Actor(93, "isha")); actors.add(new Actor(8, "deepak")); actors.add(new Actor(44, "yogi")); actors.add(new Actor(87, "yashi")); return actors; } } class Actor { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Actor(int age, String name) { this.name = name; this.age = age; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Before Sorting: 5---yashu 64---gaurangee 22---pulkit 93---isha 8---deepak 44---yogi 87---yashi After Sorting: 5---yashu 8---deepak 22---pulkit 44---yogi 64---gaurangee 87---yashi 93---isha |
Sorting of a Stream in Java 8...!!! Share on X
Do you like this Post? – then check my other helpful posts: