How Sorting In Java Collections Work

Introduction

In this article we discuss sorting in Java collections.

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.

import java.util.*;

class SortingEx1

  {

    public static void main(String args[])

      {

        ArrayList arylst=new ArrayList();

        arylst.add("Paul");

        arylst.add("John");

        arylst.add("Paul");

        arylst.add("San");

        Collections.sort(arylst);

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

      }

  }

Output

fig-1.jpg

Example

In this example, we can sort elements of a List that contains Wrapper class objects:

import java.util.*;

class SortingEx2

  {

    public static void main(String args[])

      {

        ArrayList arylst=new ArrayList();

        arylst.add(Integer.valueOf(10));

        arylst.add(Integer.valueOf(21));

        arylst.add(27);

        Collections.sort(arylst);

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

      }

  }

Output

Fig-2.jpg

Comparable Interface

The Comparable Interface:

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

class Children implements Comparable

  {

    int rllno;

    String nme;

    int age;

    Children (int rllno, String nme, int age)

      {

        this.rllno=rllno;

        this.nme=nme;

        this.age=age;

      }

    public int compareTo(Object obj)

      {

        Children ch=(Children)obj;

        if(age==ch.age)

        return 0;

        else if(age>ch.age)

        return 1;

        else

        return -1;

      }

  }

SortingEx3.java

This class is designed to sort the student data on the basis of age.

import java.util.*;

import java.io.*;

class SortingEx3

  {

    public static void main(String args[])

      {

        ArrayList arylst=new ArrayList();

        arylst.add(new Children(11,"Paul",21));

        arylst.add(new Children(16,"San",27));

        arylst.add(new Children(18,"John",24));

        Collections.sort(arylst);

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            Children ch=(Children)itrtr.next();

            System.out.println(ch.rllno+" "+ch.nme+" "+ch.age);

          }

      }

  }

Output

fig-3.jpg