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.Cloneable, java.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 :
- Create Vector object of type String
- Add String element/objects to newly created vector object in Step 1
- Sort Vector using Collections.sort();
- 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:
- Java – Sorting ArrayList using Comparable and Comparator
- Java – Sorting ArrayList in descending order
- Java – How to sort LinkedList using Collections.sort() method ?
- Java – How to sort Vector using Collections.sort() method ?
- Java – Sorting list of objects on multiple fields using Comparator
- Java – Sorting HashSet contents in ascending and descending order
- Java – How to Sort HashSet in 2 ways ?
- Java 8 – How to sort HashSet ?
- Java – How to sort LinkedHashSet contents ?
- Java – How to sort TreeSet in descending order using Comparator ?
- Java – Sorting Collection of String, StringBuffer and StringBuilder
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html
Happy Coding !!
Happy Learning !!