Vectors – Java Collections Framework

Vectors - Featured Image

Hello guys, I am back with my 3rd article in this Java Collections Framework article series. Today, I am going to talk about a class you may not have heard about that often. It is called Vector. Let’s get into the article and explore what Vector can do for us.

What is a Vector?

Vector class implements the List Interface. This reminds me that after this article we will have completed talking about all the classes with Java Collections Framework that implements the List Interface in Java. I am thinking of writing a special article about List Interface next time.

Vector can grow or shrink according to the number of elements just like in an ArrayList. And there is a good reason why we haven’t heard about this class often. This class is not being used in a non-thread environment because it is a synchronized class. This makes the class perform poorly than fellow classes like ArrayLists.

Creating Vector Objects

There are three eligible ways to create a vector object.

1. First Method
Vector myVector = new Vector();

This creates a vector with an initial capacity of 10 elements. And when we insert the 11th element, vector increases its capacity by doubling the current capacity which makes the capacity to 20. Take a look at this code snippet.

        //Creating a vector object
        Vector myVector = new Vector();

        //Adding elements
        myVector.add("1");
        myVector.add("2");
        myVector.add("3");
        myVector.add("4");
        myVector.add("5");
        myVector.add("6");
        myVector.add("7");
        myVector.add("8");
        myVector.add("9");
        myVector.add("10");

        System.out.println("The number of elements is " + myVector.size() + " and the capacity is " + myVector.capacity());
        myVector.add("11");
        System.out.println("The number of elements is " + myVector.size() + " and the capacity is " + myVector.capacity());

Output:

The number of elements is 10 and the capacity is 10
The number of elements is 11 and the capacity is 20
2. Second Method
Vector myVector = new Vector(5);

This creates a vector object with a capacity of 5 elements. And when we insert the 6th element, the capacity will be set to 10 just like in the earlier example. Take a look this code snippet for more clarification

        //Creating a vector object
        Vector myVector = new Vector(5);

        //Adding elements
        myVector.add("1");
        myVector.add("2");
        myVector.add("3");
        myVector.add("4");
        myVector.add("5");

        System.out.println("The number of elements is " + myVector.size() + " and the capacity is " + myVector.capacity());
        myVector.add("6");
        System.out.println("The number of elements is " + myVector.size() + " and the capacity is " + myVector.capacity());

Output:

The number of elements is 5 and the capacity is 5
The number of elements is 6 and the capacity is 10
3. Third Method
Vector myVector = new Vector(5, 10);

This sets the capacity to 5 and capacity increment to 10. Look at this code snippet.

        //Creating a vector object
        Vector myVector = new Vector(5, 10);

        //Adding elements
        myVector.add("1");
        myVector.add("2");
        myVector.add("3");
        myVector.add("4");
        myVector.add("5");

        System.out.println("The number of elements is " + myVector.size() + " and the capacity is " + myVector.capacity());
        myVector.add("6");
        System.out.println("The number of elements is " + myVector.size() + " and the capacity is " + myVector.capacity());

Output:

The number of elements is 5 and the capacity is 5
The number of elements is 6 and the capacity is 15

Adding Elements

        //Adding elements - First Way
        System.out.println(myVector.add("Finch")); //Prints true to the console
        myVector.add("Warner");
        myVector.add("Smith");
        myVector.add("Marnus");
        System.out.println("The list after first set : " + myVector);

        //Adding elements - Second Way
        myVector.add(2, "Marsh"); // Adds Marsh to the 2nd index
        System.out.println("The list after second set : " + myVector);

Output:

true
The list after first set : [Finch, Warner, Smith, Marnus]
The list after second set : [Finch, Warner, Marsh, Smith, Marnus]

Retrieving Elements

        //Getting elements with index
        System.out.println("The person in the 2nd index is : " + myVector.get(2));

Output:

The person in the 2nd index is : Marsh

Removing Elements

        System.out.println(myVector);
        
        //Removing elements from the vector
        System.out.println(myVector.remove("Warner")); // This prints true

        System.out.println(myVector);

Highlighted line prints true to the screen since the removal process was done successfully. Here is the console output :

[Finch, Warner, Marsh, Smith, Marnus]
true
[Finch, Marsh, Smith, Marnus]

Other Useful Methods

1. int capacity()

This returns the current capacity of the vector

2. void clear()

This removes all the elements from the vector

3. boolean contains (Object obj)

This returns true if the specified element is there in the vector

4. Element elementAt(int index)

This returns the element at the specified index of the vector.

5. int indexOf (Object obj, int index)

This returns the index of the first occurrence of a specified object in the vector. And the searching starts from the index we specify. This returns -1 if the element was not found

6. void trimToSize()

This trims the capacity of the vector to its current size


This takes us to the end of the article about Vectors in Java. I hope you guys learned something valuable. If you are new, please follow the blog to receive email notifications when I publish new articles. If you liked the article, go ahead and drop a like and share it among your friends. Stay Safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: