Tutorials

Java Collection Framework


Collection sorting


In Java, we can sort the following objects:

  • String class objects
  • Wrapper class objects
  • User defined objects

Sorting String and Wrapper class object is possible using sort() method provided by Collections class. We will learn in detail about them with example in this chapter.

 

But user-defined objects cannot be sorted by sort(). We need to use comparable or comparator interface to sort these type of objects which we will discuss in next chapters.

 

Sorting String/Wrapper class object

We can use sort method of Collection class to sort either String object or Wrapper class object.

public void sort(List list)

The list that you provide as a parameter in the sort method must be of Comparable type.

We have already learned that both String and Wrapper class implements the comparable interface. So both the String and Wrapper class objects are of Comparable type and any list containing these objects can directly be passed as a parameter in sort(List list) method.

Example: Sort list having String Object

See the code snippet given below



package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class SortArrayList {

       public static void main(String[] args) {

           //create Array List
             ArrayList javaRadarList = new ArrayList();

           //Add elements to ArrayList
             javaRadarList.add("Java");
             javaRadarList.add("Jquery");
             javaRadarList.add("Spring");
             javaRadarList.add("Hibernate");
             javaRadarList.add("EJB");

             System.out.println("Before sorting: " +javaRadarList);

              Collections.sort(javaRadarList); //sorting list element
              
              //use iterator to traverse list
              Iterator itrAfterSort = javaRadarList.iterator();

              System.out.println("After Sorting:");

              while(itrAfterSort.hasNext()){

               String token = itrAfterSort.next();
               System.out.println(token);

              }

       }

}

OUTPUT

Before sorting: [Java, Jquery, Spring, Hibernate, EJB]
After Sorting:
EJB
Hibernate
Java
Jquery
Spring
 

Example: Sort list having Wrapper class object

See the code snippet given below


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class testClass {

      public static void main(String[] args) {

            //create Array List
            ArrayList javaRadarList = new ArrayList();

            //Add elements to ArrayList
            javaRadarList.add(Integer.valueOf(6));
            javaRadarList.add(Integer.valueOf(2));
            javaRadarList.add(Integer.valueOf(3));
            javaRadarList.add(Integer.valueOf(1));
            javaRadarList.add(Integer.valueOf(4));

            System.out.println("Before sorting: " +javaRadarList);

            Collections.sort(javaRadarList); //sorting list element

            //use iterator to traverse list
            Iterator itrAfterSort = javaRadarList.iterator();

            System.out.println("After Sorting:");

            while(itrAfterSort.hasNext()){

                  Integer token = (Integer) itrAfterSort.next();

                  System.out.println(token);

            }

      }

}

OUTPUT

Before sorting: [6, 2, 3, 1, 4]
After Sorting:
1
2
3
4
6

 

 



Please Share this page
Views : 43