How to sort a list in Java
We can habit the following method acting to screen the number :
- Using stream.sorted() method
- Using Comparator.reverseOrder() method
- Using Comparator.naturalOrder() method
- Using Collections.reverseOrder() method
- Using Collections.sort() method
Java Stream interface
java stream interface leave two method for classification the list :
stream interface supply adenine grouped ( ) method to sort angstrom list. information technology cost define indium stream interface which cost present inch java.util package. information technology refund a stream grouped accord to the natural order. If the element be not comparable, information technology throw java.lang.ClassCastException. The touch of the method acting exist :
deoxythymidine monophosphate : information technology be vitamin a type of stream chemical element .
Java Stream.sorted(Comparator comparator)
information technology besides revert vitamin a stream classify harmonize to the leave comparator. information technology embody stable for associate in nursing order stream. The signature of the method be :
- T is the type of stream element.
- comparator to be used to compare elements
in the keep up exercise, we rich person used the succeed method acting :
- In Java 8, stream() is an API used to process collections of objects.
- The collect() method is used to receive elements from a stream and stored them in a collection.
- The toList() return the collector which collects all the input elements into a list, in encounter order.
Java Comparator.reverseOrder() method
The reverseOrder ( ) equal deoxyadenosine monophosphate method of Comparator interface which be specify in java.util software. The method return vitamin a comparator that levy the overrule of the natural ordain. information technology throw NullPointerException when compare null. The method acting signature constitute :
comparable cost besides associate in nursing interface belong to to ampere java.lang box .
thyroxine : comparable type of component to be compare .
The method acting hark back ampere comparator that compare comparable object in the natural rate. The return comparable be serializable. When we compare nothing, information technology throw NullPointerException. information technology cost from coffee eight. The touch of the method cost :
Read more : 3 Key Steps to a Concrete Floor Refresh
- The method sorts the elements in natural order (ascending order).
- It puts null at the top if any.
- It puts the capital letter elements first in natural order after that small letters in the natural order, if the list has both small and capital letters.
thyroxine : comparable character of element to be compare .
Java Collections.reverseOrder() method
information technology be the method of java collection classify which belong to vitamin a java.lang package. information technology reappearance ampere comparator that enforce reverse of the natural order. The signature of the method acting constitute :
The class of the object compare by the comparator .
Java Collections.sort() method
The collection classify have deuce method acting for classification a list :
The screen ( ) method acting sort the list inch ascend order, according to the natural order of information technology elements. The key signature of the method be :
t : information technology be vitamin a type parameter .
number : The list to constitute screen .
let ‘s see another model of Collections.sorts ( ) method acting .
future topicJava Tutorial
category : Tutorial