Java – How to sort Vector using Collections.sort() method ?

In this article, we will list down steps to sort Vector

1. Vector:

  • Vector is implementation class of List interface (i.e.; Vector implements List)
  • Vector uses resizable array or grow-able array to store element/objects
  • Duplicate element/objects are allowed to be inserted
  • Insertion order is maintained
  • Allows NULL insertion (no limit, any number of NULL insertion is allowed)
  • Without generics, Vector allows to insert any type of objects; with generics, it is type-bounded (except, if we take Object as type within angle brackets)
  • Elements retrieval is faster as Vector works on zero-based index
  • Manipulation (i.e.; addition/deletion of element from middle of Vector) is very slow, as it requires lot of shifting work internally
  • Note: this is exactly same as that of ArrayList except all methods of Vector class is synchronized. Hence, it is thread-safe
  • Present in java.util package and extends java.util.AbstractList implements java.util.Listinterface
  • Also, implements java.util.RandomAccess, java.lang.Cloneablejava.io.Serializablemarker interfaces which provides special ability to Vector (provided by JVM at run time)
  • Random Access: to access any random element/objects with same speed
  • Cloneable: to create a duplicate object or to clone an object
  • Serializable: to transfer objects across network

2. Steps for Vector Sorting :

  1. Create Vector object of type String
  2. Add String element/objects to newly created vector object in Step 1
  3. Sort Vector using Collections.sort();
  4. Finally display Vector iterating through for-each loop

VectorSorting.java

package in.bench.resources.vector.sorting;

import java.util.Collections;
import java.util.Vector;

public class VectorSorting {

	public static void main(String[] args) {

		// Step 1 - create Vector object of type String
		Vector<String> vector = new Vector<String>();

		// Step 2 - add elements to Vector
		vector.add("RajiniKanth");
		vector.add("KamalHaasan");
		vector.add("AjithKumar");
		vector.add("Vijay");
		vector.add("Vikram");
		vector.add("Suriya");
		vector.add("Dhanush");
		vector.add("STR");

		// Step 2.a - display before sorting
		System.out.println("Vector in insertion-order"
				+ " - before Sorting \n");
		for(String actorName : vector) {
			System.out.println(actorName);
		}

		// Step 3 - default sorting of vector in natural-order
		Collections.sort(vector);

		// Step 4 -  display after sorting
		System.out.println("\n\nVector in alphabetically"
				+ " natural-order - after Sorting\n");
		for(String actorName : vector) {
			System.out.println(actorName);
		}
	}
}

Output:

Vector in insertion-order - before Sorting 

RajiniKanth
KamalHaasan
AjithKumar
Vijay
Vikram
Suriya
Dhanush
STR

Vector in alphabetically natural-order - after Sorting

AjithKumar
Dhanush
KamalHaasan
RajiniKanth
STR
Suriya
Vijay
Vikram

Note: All methods of Vector are synchronized hence it is thread-safe

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - How to find duplicate in String[] Arrays ?
Java - Sorting Collection of String, StringBuffer and StringBuilder