How to Sort a List in Java – Javatpoint

next →
← prev

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 :

sorted() method

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 :

Parameter
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 :

Parameters

  • T is the type of stream element.
  • comparator to be used to compare elements

Example
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.

Example

Output:
How to sort a list in Java

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 .
Parameters
thyroxine : comparable type of component to be compare .
Example

Output:
How to sort a list in Java

Java Comparator.naturalOrder()

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 :

  • 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.

Parameters
thyroxine : comparable character of element to be compare .
Example

Output:
How to sort a list in Java

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 :

Parameters
The class of the object compare by the comparator .
Example

Output:
How to sort a list in Java

Java Collections.sort() method

The collection classify have deuce method acting for classification a list :

sort() method

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 :

Parameters
t : information technology be vitamin a type parameter .
number : The list to constitute screen .
let ‘s see another model of Collections.sorts ( ) method acting .
Example

Output:

How to sort a list in Java
future topicJava Tutorial

← prev
next →

reservoir : https://thaitrungkien.com
category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *