In this tutorial, we will discuss the sort list in Java with the help of examples. We will also discuss how to use it in our own way to implement the Java sort list of objects. Hence, list is an interface, and we usually apply one of its applications, such as linked list or array list.
Here are the following methods for sorting of the list in Java:
- Stream.sorted() method
- Comparator.reverseOrder() method
- Comparator.naturalOrder() method
- Collections.reverseOrder() method
- Collections.sort() method
Using Java stream.sorted() method
In Java Stream.sorted() method it will give two methods for sorting the list. The stream interface will give a sorted() method for sorting a list. It will specify in Stream interface in java.util package. When the elements are not equal, it will send java.lang.ClassCastException.
The syntax method is the following below:
Stream<S> sorted()
Parameter
S: This is a type of stream element.
Using Java Stream.sorted(Comparator comparator)
The comparator.reverseOrder() method will provide a stream sorted that is sorted using the defined comparator.
The syntax used is the following:
Stream<R> sorted(Comparator<? Strong R> comparator)
Parameter
- R is a type of stream element.
- We can use comparator to compare elements.
For example, we will use the following method:
- Stream() is an API that handles groupings of objects in Java 8.
- We will use the collect() method to receive elements within a stream and store them in a compilation.
- The collector that compiles all of the input elements into a list in encounter order is returned by the toList() function.
import java.util.*;
import java.util.stream.*;
public class SortListExample1
{
public static void main(String[] args)
{
//returns a list view
List<String> Namelist = Arrays.asList("Paul", "Glenn", "Caren", "Elijah", "Jude", "Adonis");
List<String> sortedList = Namelist.stream().sorted().collect(Collectors.toList());
sortedList.forEach(System.out::println);
}
}
Output:
Using Java Comparator.reverseOrder() method
In Java program, a reverseOrder() method is a comparator interface which is specified in java.util package. The comparator method will return the reverse ordering. It will throws NullPointerException when matching a null.
The syntax used:
static <R extends Comparable<? R>> Comparator<R> reverseOrder()
The term comparable will be the interface and belong to a java.lang package.
Parameters
R: It is a comparable type of element to be matched.
Let’s look at the Example of Java Comparator.reverseOrder():
import java.util.*;
import java.util.stream.Collectors;
public class ComparatorSortListExample2
{
public static void main(String[] args)
{
//returns a list view
List<String> Comparatorlist = Arrays.asList("61", "ab", "db", "hi", "de", "hs", "70", "45", "91");
List<String>ComparatorsortedList=Comparatorlist.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
sortedList.forEach(System.out::println);
}
}
Output:
Used Comparator.naturalOrder() method
The Comparator.naturalOrder() method will return into a comparator that measures the comparable objects in a natural order. If we will comparing the null, it will throw NullPointerException.
Syntax used:
Static <R extends Comparable <? string R>> Comparator<T> naturalOrder()
Parameters
R: This is the Comparable type of element and it should be compared.
Here is the example of Comparator.naturalOrder() method:
import java.util.*;
public class ComparableNaturalOrderListExample
{
public static void main(String[] args)
{
List<String> name = Arrays.asList("Jhonny", "Jhenny", "Joseph", "Jackie", "", "Jeffrey");
List<String> name1 = Arrays.asList("Jhonny", "Jhenny", "Joseph", "Jackie", "", "Jeffrey");
System.out.println(name);
name.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println(name);
name.sort(Comparator.naturalOrder());
System.out.println(name);
name1.sort(Comparator.naturalOrder());
System.out.println(name1);
}
}
Output:
Using Java Collections.reverseOrder() method
In Java program, a Collections.reverseOrder() method is a set of class which exist in a java.lang package. The comparator will return that requires a natural order.
Syntax Used:
public static <String> Comparator<String> reverseOrder()
Parameters
String: Is the class of the objects which is to be compared by the comparator.
Let’s have a look at the example of Java Collections.reverseOrder() method below:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ReverseOrderExample4
{
public static void main(String args[])
{
List<Integer> reverseOrderlist = Arrays.asList(100,20,-200,400,50,-203,0);
Collections.sort(reverseOrderlist, Collections.reverseOrder());
System.out.println(reverseOrderlist);
}
}
Output:
Using Java Collections.sort() method
In Java program, the Java Collections.sort() method is the collections of class which have two methods for sorting a list:
Java Sort() Method
A sort() method is a list of sort in ascending order, which is according to the essential ordering of its elements.
We will use the syntax:
public static <S extends Comparable<? Java S>> void sort() (List<S> list)
Parameters
S: It is a type of parameter to be compared.
List: This is the list which is to be sorted.
Let’s look at the example of the collection sorted list below:
import java.util.*;
public class ExampleSortList
{
public static void main(String[] args)
{
List<String> SortList = new ArrayList<String>();
SortList.add("4");
SortList.add("8");
SortList.add("6");
SortList.add("9");
SortList.add("3");
Collections.sort(SortList); //sorts array list
for(String str: SortList)
System.out.print(" "+str);
}
}
Output:
Conclusion
To conclude, we already learn the sort a list in java with the detailed explanation. Also we learn the methods of sort a list java like Stream.sorted() method, Comparator.reverseOrder() method, Comparator.naturalOrder() method, Collections.reverseOrder() method, and Collections.sort() method.