• September 3, 2014
  • Core Java
  • Comments Off on Sorting in collection framework

Sorting in collection framework

Sorting:

Sorting is a process of arranging data in an increasing or decreasing order.

As we discussed earlier that TreeSet and TreeMap maintains an order for its elements. But list not provides any facility of ordering the elements. Collections classes provides the sort() method to arrange list elements.

Note: By using sort() method we can sort the String and Wrapper class objects only but not user defined objects. It is because String and Wrapper classes implements Comparable interface.

Example of sorting the elements of List that contains string and Wrapper class objects:

SortingTest.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
 
/**
 * This class is used to show the sorting functionality.
 * @author javawithease
 */
public class SortingTest {
	public static void main(String args[]){
		ArrayList arrayList1 = new ArrayList();
		ArrayList arrayList2 = new ArrayList();
 
		//Add string objects to the ArrayList.
		arrayList1.add("Amani");
		arrayList1.add("Prabhjot");
		arrayList1.add("Nidhi");
		arrayList1.add("Vandana");
		arrayList1.add("Poonam");
 
		//Add Wrapper objects to the ArrayList.
		arrayList2.add(Integer.valueOf(12));
		arrayList2.add(Integer.valueOf(34));
		arrayList2.add(Integer.valueOf(14));
		arrayList2.add(Integer.valueOf(56));
		arrayList2.add(Integer.valueOf(4));
 
		//Print the Collection string elements before sorting.
		Iterator iterator1=arrayList1.iterator();
		System.out.println("Collection string elements " +
				"before sorting:");
		while(iterator1.hasNext()){  
		   System.out.println(iterator1.next());  
		}  
 
		//Call the Collections sort method for sorting
		Collections.sort(arrayList1); 
 
		//Print the Collection string elements before sorting.
		Iterator iterator2=arrayList1.iterator();
		System.out.println("Collection string elements " +
				"after sorting:");
		while(iterator2.hasNext()){  
		   System.out.println(iterator2.next());  
		}  
 
		//Print the Collection Wrapper elements before sorting.
		Iterator iterator3=arrayList2.iterator();
		System.out.println("Collection Wrapper elements " +
				"before sorting:");
		while(iterator3.hasNext()){  
		   System.out.println(iterator3.next());  
		}  
 
		//Call the Collections sort method for sorting
		Collections.sort(arrayList2); 
 
		//Print the Collection Wrapper elements before sorting.
		Iterator iterator4=arrayList2.iterator();
		System.out.println("Collection Wrapper elements " +
				"after sorting:");
		while(iterator4.hasNext()){  
		   System.out.println(iterator4.next());  
		}  		
 
	}
}

Output

Collection string elements before sorting:
Amani
Prabhjot
Nidhi
Vandana
Poonam
Collection string elements after sorting:
Amani
Nidhi
Poonam
Prabhjot
Vandana
Collection Wrapper elements before sorting:
12
34
14
56
4
Collection Wrapper elements after sorting:
4
12
14
34
56

Download this example.
 
Next Topic: Comparable interface in java with example.
Previous Topic: Abstract classes in collection framework in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.