How Sorting In Java Collections Work

Introduction

 
In this article, we discuss sorting in Java collections. Sorting is ordering a list of objects. We can distinguish two types of sorting. If the number of objects is small enough to fits into the main memory, sorting is called internal sorting. If the number of objects is so large that some of them reside on external storage during the sort, it is called external sorting.
 

Sorting

 
We can sort the elements of the following object classes:
  1. Wrapper class.
  2. String class.
  3. user-defined class.
The Collections class has static methods for sorting the elements. If the elements of a collection are of the Set type then we can use the TreeSet class to sort it. But its not possible to sort the elements of a List. The Collections class provides methods for sorting the elements of List type elements.
 
For sorting the List elements some public methods of the Collections class are:
 
void sort (List list)
              used to sort the elements of List. List elements must be of Comparable type.
 
Example: In this example, we can sort the elements of a List that contains string objects.
  1. import java.util.*;  
  2. class SortingEx1 {  
  3.  public static void main(String args[]) {  
  4.   ArrayList arylst = new ArrayList();  
  5.   arylst.add("Paul");  
  6.   arylst.add("John");  
  7.   arylst.add("Paul");  
  8.   arylst.add("San");  
  9.   Collections.sort(arylst);  
  10.   Iterator itrtr = arylst.iterator();  
  11.   while (itrtr.hasNext()) {  
  12.    System.out.println(itrtr.next());  
  13.   }  
  14.  }  
  15. }   
Output
 
fig-1.jpg
Example: In this example, we can sort elements of a List that contains Wrapper class objects:
  1. import java.util.*;  
  2. class SortingEx2 {  
  3.  public static void main(String args[]) {  
  4.   ArrayList arylst = new ArrayList();  
  5.   arylst.add(Integer.valueOf(10));  
  6.   arylst.add(Integer.valueOf(21));  
  7.   arylst.add(27);  
  8.   Collections.sort(arylst);  
  9.   Iterator itrtr = arylst.iterator();  
  10.   while (itrtr.hasNext()) {  
  11.    System.out.println(itrtr.next());  
  12.   }  
  13.  }  
  14. }  
Output
 
Fig-2.jpg

Comparable Interface

 
The Comparable Interface:
  • It's in the java.lang package.
  • Sorts the objects of a user-defined class.
  • Contains only a single method named compareTo(Object).
  • Provides only a single sorting sequence.
Syntax
 
public int compareTo(Objects obj) 
              used to compare the current object with the specified object.
  
Example: In the following example, we can sort the elements of a List that contains user-defined class objects on age basis.
 
Children.java
 
This class contains a student's records, in other words, their age, name, and rollno.
  1. class Children implements Comparable {  
  2.  int rllno;  
  3.  String nme;  
  4.  int age;  
  5.  Children(int rllno, String nme, int age) {  
  6.   this.rllno = rllno;  
  7.   this.nme = nme;  
  8.   this.age = age;  
  9.  }  
  10.  public int compareTo(Object obj) {  
  11.   Children ch = (Children) obj;  
  12.   if (age == ch.age)  
  13.    return 0;  
  14.   else if (age > ch.age)  
  15.    return 1;  
  16.   else  
  17.    return -1;  
  18.  }  
  19. }  
SortingEx3.java
 
This class is designed to sort the student data on the basis of age.
  1. import java.util.*;  
  2. import java.io.*;  
  3. class SortingEx3 {  
  4.  public static void main(String args[]) {  
  5.   ArrayList arylst = new ArrayList();  
  6.   arylst.add(new Children(11"Paul"21));  
  7.   arylst.add(new Children(16"San"27));  
  8.   arylst.add(new Children(18"John"24));  
  9.   Collections.sort(arylst);  
  10.   Iterator itrtr = arylst.iterator();  
  11.   while (itrtr.hasNext()) {  
  12.    Children ch = (Children) itrtr.next();  
  13.    System.out.println(ch.rllno + " " + ch.nme + " " + ch.age);  
  14.   }  
  15.  }  
  16. }  
Output
 
fig-3.jpg


Similar Articles