How to use Vector collection implementation in Java environment


Vector collection is an implementation of the List interface in Java Collection Framework.
Vector is also a synchronized collection that could be used in thread safe applications.
Because of that it has slower performance than the ArrayList implementation of the List Interface.

Here is an example program for the usage of Vector collection class in Java


/*
 *How to use a Vector implementation of Java Collection Framework
 */

package vectorexample;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author tunatore
 */

public class VectorExample {

public static void main(String[] args) {

System.out.println("==========VECTOR EXAMPLE==========");
List<String> vectorOne = new Vector<String>();        
vectorOne.add("this is the first element for the VectorOne");
vectorOne.add("this is the second element for the VectorOne");
vectorOne.add("this is the third element for the VectorOne");

Vector<String> vectorTwo = new Vector<String>();
vectorTwo.add("this is the first element for the vectorTwo");
vectorTwo.add("this is the second element for the vectorTwo");

System.out.println("The size of the vectorOne --> " + vectorOne.size());
System.out.println("The size of the vectorTwo --> " + vectorTwo.size());

System.out.println("==========Getting the elements inside vectorTwo==========");

for (Iterator<String> it = vectorTwo.iterator(); it.hasNext();) {
String element = it.next();
System.out.println(element);
}

System.out.println("==========Testing an element existince in vectorTwo==========");
System.out.println("Exist or Not --> " + vectorTwo.contains("this is the second element for the vectorTwo"));

System.out.println("==========Removing an element using index remove(i) in vectorTwo==========");
vectorTwo.remove(1); // 1 refers to the second element in vectorTwo

System.out.println("==========Testing againg an element existince in vectorTwo==========");
System.out.println("Exist or Not --> " + vectorTwo.contains("this is the second element for the vectorTwo"));

System.out.println("==========Adding two more elements in vectorTwo==========");
vectorTwo.add("this is the second element for the vectorTwo");
vectorTwo.add("this is the third element for the vectorTwo");

System.out.println("==========Getting the first element of vectorTwo==========");
System.out.println("The first element ==> " + vectorTwo.firstElement());

System.out.println("==========Getting the last element of VectorOne==========");
System.out.println("The last element ==> " + vectorTwo.lastElement());
}
}

The Output for the program;

run:
==========VECTOR EXAMPLE==========
The size of the vectorOne –> 3
The size of the vectorTwo –> 2
==========Getting the elements inside vectorTwo==========
this is the first element for the vectorTwo
this is the second element for the vectorTwo
==========Testing an element existince in vectorTwo==========
Exist or Not –> true
==========Removing an element using index remove(i) in vectorTwo==========
==========Testing againg an element existince in vectorTwo==========
Exist or Not –> false
==========Adding two more elements in vectorTwo==========
==========Getting the first element of vectorTwo==========
The first element ==> this is the first element for the vectorTwo
==========Getting the last element of VectorOne==========
The last element ==> this is the third element for the vectorTwo
BUILD SUCCESSFUL (total time: 0 seconds)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s