Collection Framework – Vector in java

Vector in java



vector in java

 

vector in java

  • Vector is implementation class of List interface like ArrayList.
  • Vector implements three marker interface
    — Serializable
    — Cloneable
    — Random access
  • Vector is a legacy class which is introduced in java 1.0
  • All method in vector is synchronized so it is thread safe class.
  • Vector size grows by doubling the capacity(100%).

Note:  Vector is slow because it is synchronized i.e. in multi-threading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.


Three different ways of creating a Vector class object:

  1. Vector vect = new Vector(): It will create an empty Vector with default capacity of 10. But when you will add 11th element it will doubles it size to 20, Because it is the property of Vector.
  2. Vector vobj= new Vector(int initialCapacity): In this case you can assign the capacity of the Vector manually. If you will assign initialCapacity=5, then it will create a Vector of capacity 5. eg: Vector vobj= new Vector(5)
  3. Vector object= new vector(int initialcapacity, capacityIncrement): Two arguments are there in this case, one for initial capacity and other for increment in capacity after adding one more element than the initial capacity. eg: Vector object= new vector(8, 7). So here initial capacity is 8, if we will 9th element it’s capacity will become 15.

Example of Vector in Java

[pastacode lang=”java” manual=”package%20collectionConcept%3B%0Aimport%20java.util.Vector%3B%0A%0Apublic%20class%20Vector1%0A%20%7B%0A%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%7B%0A%20%20%20%20System.out.println(%22*************************%22)%3B%0A%0A%20%20%20%20Vector%3CString%3E%20vect%20%3Dnew%20Vector%3CString%3E()%3B%0A%20%20%20%20vect.add(%22abhilash%22)%3B%20%20%2F%2Fmethod%20of%20Collection%20Interface%0A%20%20%20%20vect.add(%22bhavesh%22)%3B%0A%20%20%20%20vect.addElement(%22rohit%22)%3B%20%20%2F%2Fmethod%20of%20Vector%20%20class%0A%20%20%20%20vect.addElement(%22hardik%22)%3B%0A%20%20%20%20vect.addElement(%22jspiders%22)%3B%0A%0A%20%20%20%20System.out.println(vect.size())%3B%0A%0A%20%20%20%2F%2Fsetting%20value%20at%20index%204%0A%20%20%20vect.set(4%2C%20%22abhilash%22)%3B%0A%0A%20%20%20%2F%2Ftraversing%20elements%20using%20Enumeration%20%20%0A%20%20%20Enumeration%20e%3Dvect.elements()%3B%20%20%0A%20%20%20while(e.hasMoreElements())%7B%20%20%0A%20%20%20%20%20System.out.println(e.nextElement())%3B%20%20%0A%20%20%20%7D%0A%20%20%7D%0A%7D” message=”Understanding vector in Java through a program” highlight=”” provider=”manual”/]

output:
*************************
5
abhilash
bhavesh
rohit
hardik
abhilash

Some Important Methods of Class Vector in Java

  • void addElement(Object element): This method inserts the element at the end of the Vector.
  • int capacity(): This method returns the current capacity of the vector.
  • int size(): This method returns the current size of the vector.
  • void setSize(int size): It changes the existing size with the specified size.
  • boolean contains(Object element): This method checks whether the specified element is present in the Vector. If the element is been found it returns true else false.
  • boolean containsAll(Collection c): It returns true if all the elements of collection c are present in the Vector.
  • Object elementAt(int index): It returns the element present at the specified location in Vector.
  • Object firstElement(): It is used for getting the first element of the vector.
  • Object lastElement(): It returns the last element of the array.
  • Object get(int index): It returns the element at the specified index.
  • boolean isEmpty(): It returns true if Vector doesn’t have any element.
  • boolean removeElement(Object element): This method removes the specified element from vector.
  • boolean removeAll(Collection c): It Removes all those elements from vector which are present in the Collection c.
  • void setElementAt(Object element, int index): It updates the element of specified index with the given element.

Read about Linked list in Java….

Linkedlist in Java

Leave a Reply