This example shows how to sort the elements of the LinkedList in Java. The example also shows how to sort the LinkedList using the Comparator and Comparable interfaces.
How to sort LinkedList in Java?
We can use the sort
method of the Collections class to sort the elements of the LinkedList.
1 |
public static <T extends Comparable<? super T>> void sort(List<T> list) |
The above method takes a List as an argument. Since the LinkedList class implements the List interface, we can use this method to sort the LinkedList 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 |
import java.util.Collections; import java.util.LinkedList; public class LinkedListSortExample { public static void main(String[] args) { //create new LinkedList object LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); //add elements linkedListNumbers.add(23); linkedListNumbers.add(45); linkedListNumbers.add(12); linkedListNumbers.add(37); linkedListNumbers.add(49); /* * To sort the linked list, use * the sort method of the Collections class. */ Collections.sort( linkedListNumbers ); System.out.println(linkedListNumbers); } } |
Output
1 |
[12, 23, 37, 45, 49] |
As you can see from the output, the linked list elements are sorted in ascending order by the sort
method. The sort
method orders the elements in their natural order which is ascending order for the type Integer.
Note: The LinkedList elements must implement the Comparable interface for this method to work. The below given example shows how to do that in a custom class.
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 |
import java.util.Collections; import java.util.LinkedList; class Student implements Comparable<Student>{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } /* * Implement this method in your custom class. * * Compare this student object with another one. * This example compares the ids of the student. */ public int compareTo(Student otherStudent) { return this.id - otherStudent.id; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } } public class LinkedListSortExample { public static void main(String[] args) { //create new LinkedList object LinkedList<Student> linkedListStudents = new LinkedList<Student>(); linkedListStudents.add( new Student(4, "Jack") ); linkedListStudents.add( new Student(3, "John") ); linkedListStudents.add( new Student(1, "Emily") ); linkedListStudents.add( new Student(5, "Maria") ); linkedListStudents.add( new Student(2, "Sheila") ); /* * This will use the compareTo method defined in * the Students class */ Collections.sort(linkedListStudents); System.out.println("Sorted LinkedList elements"); System.out.println(linkedListStudents); } } |
Output
1 2 |
Sorted LinkedList elements [[1=>Emily], [2=>Sheila], [3=>John], [4=>Jack], [5=>Maria]] |
As you can see from the output, the compareTo
method defines the natural ordering of the Student objects.
How to sort the LinkedList containing custom class objects using a Comparator?
We have seen how to sort the linked list elements using the Comparable interface. In the following example, I will show you how to sort the LinkedList using the Comparator interface. I am going to use the same Student class for this example.
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 72 73 74 75 76 77 78 |
class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } public Integer getId(){ return this.id; } public String getName(){ return this.name; } } /* * A custom comparator that sorts the student objects * in the ascending order */ class StudentAscendingComparator implements Comparator<Student>{ public int compare(Student student1, Student student2) { return student1.getId() - student2.getId(); } } /* * A custom comparator that sorts the student objects * in the descending order */ class StudentDescendingComparator implements Comparator<Student>{ public int compare(Student student1, Student student2) { return student2.getId() - student1.getId(); } } public class LinkedListSortExample { public static void main(String[] args) { //create new LinkedList object LinkedList<Student> linkedListStudents = new LinkedList<Student>(); linkedListStudents.add( new Student(4, "Jack") ); linkedListStudents.add( new Student(3, "John") ); linkedListStudents.add( new Student(1, "Emily") ); linkedListStudents.add( new Student(5, "Maria") ); linkedListStudents.add( new Student(2, "Sheila") ); /* * Sort linked list elements in the * ascending order using the StudentAscendingComparator */ Collections.sort(linkedListStudents, new StudentAscendingComparator()); System.out.println("LinkedList elements sorted in ascending order"); System.out.println(linkedListStudents); /* * Sort linked list elements in the * descending order using the StudentDescendingComparator */ Collections.sort(linkedListStudents, new StudentDescendingComparator()); System.out.println("LinkedList elements sorted in descending order"); System.out.println(linkedListStudents); } } |
Output
1 2 3 4 |
Sorted LinkedList elements [[1=>Emily], [2=>Sheila], [3=>John], [4=>Jack], [5=>Maria]] Sorted LinkedList elements [[5=>Maria], [4=>Jack], [3=>John], [2=>Sheila], [1=>Emily]] |
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList
Java 8 Collections class