Vector in Java with Example - Javastudypoint

Sunday, February 24, 2019

Vector in Java with Example

Vector in Java is a class which is present in java.util package and implements the List interface. Vector implements a dynamic array which means we can grow or shrink the size of the array as per our need. Vector is similar to ArrayList, but with two major differences:

1. Vector is synchronized, hence vector object is thread-safe.
2. It contains many legacy methods which are not part of the collection framework.




Constructors in Java Vector Class:

Java Vector class contains four constructors. Here are the Vector constructors.

1. Vector(): The first constructor is used to creates a default vector, which has an initial size of 10.

2. Vector(int size): The second constructor is used to creates a vector whose initial capacity is specified by size.

3. Vector(int size, int incr): The third constructor is used to creates a vector whose initial capacity is specified by size and whose increment is specified by incr.

4. Vector(Collection c): The fourth constructor is used to creates a vector that contains the elements of collection c.

All the vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for those objects.



Methods of Java Vector Class:

Some of the important methods of Vector class is given below:

1. void addElement(Object element): This method is used to insert the element at the end of the vector.

2. int capacity(): This method is used to find out the current capacity of the vector.

3. boolean contains(Object element): This method is used to check the specified element present in the vector or not. If the element is found it returns true else false.

4. Object elementAt(int index): This method returns the element which is present at the specified index.

5. Object firstElement(): This method is used to retrieve the first element from the vector.

6. Object lastElement(): This method is used to retrieve the last element from a vector.

7. boolean isEmpty(): This method is used to check the vector is empty or not. It returns true if the vector doesn't have any element.

8. int indexOf(Object element): This method is used to find the index of the specified element. If the specified element is not in the vector it returns -1.

9. void insertElementAt(Object element, int index): This method is used to insert the specified element at the specified index.

10. void removeAllElements(): This method is used to remove all the elements from the vector.

11. boolean removeElement(Object element): This method is used to remove the specified element from the vector.

12. void removeElementAt(int index): This method is used to remove the element at the specified index.

13. void setElementAt(Object element, int index): This method is used to set the element at the specified index.

14. void setSize(int size): This method is used to set the size of the given vector. If the new size is less than the old size then elements are lost. If the new size is larger than the old size then null elements are added.

15. int size(): This method returns the number of elements currently in the vector.

16. int capacity(): This method returns the current capacity of the vector.

17. int hashcode(): This method returns the hashcode value of the vector.

18. Object get(int index): This method returns an element which is present at the specified index.


Java Vector example to add elements:


import java.util.*;
class Example
{
public static void main(String args[])
 {
//creating a vector
Vector vec = new Vector(5);

//adding element to the vector
vec.addElement("Sumit");
vec.addElement("Amit");
vec.addElement("Rohit");
vec.addElement("Rahul");
vec.addElement("Ajay");

//check size and capacity of a vector

System.out.println("Size of the Vector is: " +vec.size());

System.out.println("Default increment of a Vector is: " +vec.capacity());


vec.addElement("Vijay");
vec.addElement("Prashant");
vec.addElement("Raaj");


//again check size and capacity of a vector after adding 3 element

System.out.println("After the addition the size is: " +vec.size());

System.out.println("The increment of vector is: " +vec.capacity());

}
}


Output:

Size of the vector is: 5
Default increment of a vector is: 5
After the addition the size is: 8
The increment of the vector is: 10


Java Vector Example to remove elements:

import java.util.*;

class Example
{
public static void main(String args[])
{
//creating a vector
Vector<String> vec = new Vector<String>();

//adding element to the vector
vec.addElement("Sumit");
vec.addElement("Amit");
vec.addElement("Rohit");
vec.addElement("Rahul");
vec.addElement("Ajay");
vec.addElement("Vijay");
vec.addElement("Raam");

System.out.println("Elements of Vector before remove() is:\n "); 
for(int i=0; i<vec.size(); i++)
{
System.out.println(vec.get(i)); 
}

//remove two elements
vec.remove("Amit"); 
vec.remove("Rahul"); 

System.out.println("Elements of vector after remove() is \n"); 
for(int i=0; i<vec.size(); i++)
{
System.out.println(vec.get(i)); 
}

Object obj = vec.remove(2);
System.out.println("Element of vector at a given index is \n"); 
System.out.println(obj);

//removing all the elements
vec.clear();
System.out.println("Size of the vector after removing all the elements" + vec.size());
} 
}

Output:

Elements of the vector before remove(): 
Sumit
Amit
Rohit
Rahul
Ajay
Vijay
Raam

Elements of the vector after remove():
Sumit
Rohit
Ajay
Vijay
Raam

Element of the vector at a given index: Ajay
Size of the vector after removing all the elements: 0



Java Vector Example to get the hashcode of the vector:



import java.util.*;

class Example
{
public static void main(String args[])
{

//creating a vector
Vector vec = new Vector();

//adding element to the vector
vec.addElement("Sumit");
vec.addElement("Amit");
vec.addElement("Rohit");
vec.addElement("Rahul");
vec.addElement("Ajay");
vec.addElement("Vijay");
vec.addElement("Raam");

//get the hashcode of this vector
System.out.println("Hashcode of this vector is: " +vec.hashCode()); 


}


Output:
Hashcode of this vector is: 938853329


Java Vector Example to convert Vector into a List:

We can convert a vector into a List by calling Collections.list(vec.elements()), It returns a List object.
import java.util.*;

class Example

{

public static void main(String args[])

{

//creating a vector

Vector vec = new Vector();


//adding the element to the vector

vec.addElement("Sumit");

vec.addElement("Amit");

vec.addElement("Rohit");

vec.addElement("Rahul");

vec.addElement("Ajay");

vec.addElement("Vijay");

vec.addElement("Raam");


System.out.println("Vector elements are: \n" +vec);


//converting vector to list

List<String> list = Collections.list(vec.elements());

System.out.println("List elements are:\n " +list);




Output:

Vector elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]
List elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]


Java Vector Example to convert a vector into an ArrayList:

We can convert a vector into an ArrayList by declaring an array list object using a vector object.

import java.util.*;

class Example

 {

   public static void main(String args[])

 {

 //creating a vector
Vector vec = new Vector();

//adding element to the vector
vec.addElement("Sumit");

vec.addElement("Amit");

vec.addElement("Rohit");

vec.addElement("Rahul");

vec.addElement("Ajay");

vec.addElement("Vijay");

vec.addElement("Raam");


System.out.println("Vector elements are: " +vec);


//converting a vector into an array list

ArrayList<String> al = new ArrayList<String>(vec)

System.out.println("ArrayList elements are: " +al);






Output:

Vector elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]
ArrayList elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]


You may also like:


Java Serialization with Example
LinkedList class in Java with Example
Exception Handling tutorial with Example
Encapsulation in Java OOPs with Example
Types of inheritance in Java with Example
Abstraction in Java with Example
Polymorphism in Java OOps with Example

No comments:

Post a Comment