Four programs are given on Vector to know thoroughly about Vector, methods and usage. Vector is a general purpose data structure used very often by the programmer even though the collections framework comes with many classes.
- Vector – Methods: Uses all the methods of Vector class like capacity, size, firstElement etc.
- Generics Vector: Create generics vector storing only integers and uses all Collections static methods like swap, shuttle, fill, rotate etc.
- Vector – Retrieval: Illustrates some more methods like get, remove, removeAll and also how to retrieve the elements – 6 styles
- Vector Example Special Operations: Give manipulations on vector elements like removing duplicate values, converting vector elements into an array etc.
Note: At the end of this tutorial, see the technique of avoiding duplicate elements in Vector. There is no direct method to eliminate in Vector.
It advised to go through the first program Vector Methods where vector methods, constructors and history are discussed.
Vector Example Special Operations
In this program, special operations like converting vector elements into array elements, removing duplicate elements and printing the vector elements in ascending order etc. are discussed. All are done in simple code using predefined methods of collections classes (avoiding C/C++ lengthy code of iterations)
import java.util.*;
public class VectorPlayWith
{
public static void main(String args[])
{
Vector vect1 = new Vector();
vect1.add(10);
vect1.add(20);
vect1.add(30);
vect1.add(40);
// VECTOR TO ARRAY
Object obj[] = vect1.toArray();
System.out.print("\nvect1 elements in array form: ");
for(Object o1 : obj)
{
System.out.print(o1 + " ");
}
// another style of conversion
Vector vect2 = new Vector();
vect2.add("Best");
vect2.add("Wishes");
vect2.add("From");
vect2.add("way2java.com");
String greetings[] = new String[vect2.size()];
int i = 0;
for(String s1 : vect2)
{
greetings[i++] = s1;
}
System.out.print("\nvect2 elements in array form: ");
for(String s2 : greetings)
{
System.out.print(s2 + " ");
}
// ELIMINATING DUPLICATES
Vector vect3 = new Vector();
vect3.add("apple");
vect3.add("banana");
vect3.add("apple");
vect3.add("banana");
vect3.add("strawberry");
vect3.add("apple");
System.out.println("\nvect3 elements with duplicates: " + vect3);
// converting vector to list
List myList = vect3.subList(0, vect3.size());
// convert list to set
HashSet mySet = new HashSet(myList);
// converting set back to vector
Vector vect4 = new Vector();
vect4.addAll(mySet);
System.out.println("vect4 elements without duplicates: " + vect4);
// ASCENDING ORDER
TreeSet ts = new TreeSet(myList);
System.out.println("vect4 elements in ascending order: " + ts);
}
}

Output screen of Vector Example Special Operations
Object obj[] = vect1.toArray();
vect1 elements are converted into an array with toArray() method, inherited from Collection interface. With enhanced for loop (Java 5) all the elements are iterated and printed.
String greetings[] = new String[vect2.size()];
A generics vector object vect2 is created and is converted into string array greetings. It is another style of converting vector into an array, but laborious.
Next task is eliminating duplicate elements in a vector. Generally, a programmer does a lot of loop iterations to do the task. But in Java, many methods are available in collections framework and all collections classes are interoperable. We know Set interface subclasses like TreeSet, HashSet do not accept duplicate elements. This property is taken to eliminate the duplicates. Vector is converted into HashSet. HashSet automatically removes duplicates. Again HashSet is converted back to Vector using addAll() method inherited from Collection interface.
// ELIMINATING DUPLICATES IN VECTOR
// converting list to vector
List myList = vect3.subList(0, vect3.size());
// convert list to set
HashSet mySet = new HashSet(myList);
Vector vect4 = new Vector();
vect4.addAll(mySet);
System.out.println("vect4 elemenets without duplicates: " + vect4);
Vector vect3 is created to store string objects. subList() method (inherited from List interface) is used to convert vector elements into List, myList. Again myList is converted into HashSet, mySet. mySet elements are added to another vector vect4. Now vect4 contains unique elements.
// ASCENDING ORDER
TreeSet ts = new TreeSet(myList);
System.out.println("vect4 elements in ascending order: " + ts);
For extra functionality, myList is converted into TreeSet. TreeSet nature is to print the elements in ascending order by default.
Know the tree structure of Know Java Collections Interfaces Hierarchy
i am a big fan of you and your website. can you suggest name of any institute or website for Android . i wan to earn Android .
with thanks
If you are at Hyderabad, join Durga soft, Lecturer: Sambasiva Rao