How to sort a list in Java
In this post, you will learn different ways to sort a list in the Java programming language.
The Java platform includes a collections framework. Java Collection framework provides many interfaces(List) and classes(ArrayList, Vector, LinkedList).
List in Java
A list is an ordered collection. It contains the index-based methods to insert, update, delete, and search the elements. We can access elements by their index and also search for elements in the list. A list can contain duplicate values and null elements. List interface gives positional access and insertion of elements and preserves the insertion order. The list interface extends Collection and declares the behaviour of a collection that stores a sequence of elements.
List Interface declaration
public interface List<E> extends Collection<E>
Java sort a list using java.util.Collections.sort()
The java.util.Collections.sort() method of java.util.Collections class is used to sort the elements present in the specified list of Collection in ascending order.
// Java program to sort a list
import java.util.*;
public class ListSorting
{
public static void main(String[] args)
{
// Create a list of strings
List list1 = new ArrayList();
list1.add("Guava");
list1.add("Kiwi");
list1.add("Apple");
list1.add("Pear");
list1.add("Orange");
System.out.println("The initial List:\n" + list1);
/* Collections.sort method
to sort the list */
Collections.sort(list1);
// print the sorted list
System.out.println("Sorted List :\n" + list1);
}
}
Output of the above code:
The initial List:
[Guava, Kiwi, Apple, Pear, Orange]
Sorted List :
[Apple, Guava, Kiwi, Orange, Pear]
Java sort a list using sorted() method of Stream interface
The Stream interface, which is present in the java.util package provides a sorted() method to sort a list.
Stream Interface declaration
Stream<T> sorted(Comparator<? Super T> comparator)
Here, T is the type of stream element.
In the given example, the collect() method is used to receive elements from a stream and store them in a collection. The toList() method returns the collector which collects all the input elements into a list, in encounter order.
// Java program to sort a list
import java.util.*;
import java.util.stream.*;
public class ListSorting
{
public static void main(String[] args)
{
// Create a list of strings
List list1 = new ArrayList();
list1.add("Watermelon");
list1.add("Guava");
list1.add("Pineapple");
list1.add("Papaya");
list1.add("Lemon");
System.out.println("The initial List:\n" + list1);
// print the sorted list
System.out.println("\nSorted List :\n");
List sortedList = list1.stream().sorted().collect(Collectors.toList());
sortedList.forEach(System.out::println);
}
}
Output of the above code:
The initial List:
[Watermelon, Guava, Pineapple, Papaya, Lemon]
Sorted List :
Guava
Lemon
Papaya
Pineapple
Watermelon
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number