Wednesday, 19 October 2016

What is Vector In Java ?

Previously we have discussed about
Here we learn what is Vector In Java.But before that we must know, What is Collection Interface in Java ?
Class Vector<E>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

Direct Known Subclasses:

Declaration of Vector:
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

Vector class supports growable array of objects, like an array, it contains components that can be accessed using an integer index.However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
Each vector optimize storage by maintaining a capacity and a capacityIncrement.The capacity is always same as vector size; it is usually larger because as component are added to the vector,the vector's storage increases the size of capacityIncrement.

Vector is an example of fail-fast iterator and ListIterator, if the vector is modified structurally at any time after the iterator is created,except throgh the iterator's own remove() or add() method, the iterator will throw a ConcurrentModificationException.

Vector implementation is synchronized.If a thread-safe implementation is not required, it is recommended to use ArrayList in place of Vector.
Form Java 1.2 this class retrofitted to implement the List interface,and making it a member of the Java Collections Framework. 

Constructor Summary of Vector:

Constructor
Description
Vector()
Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
Vector(Collection<? extends E> c)
Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Vector(int initialCapacity)
Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
Vector(int initialCapacity, int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment.

Method Summary of Vector:

Modifier and Method Name
Description
boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
void addElement(E obj)
Adds the specified component to the end of this vector, increasing its size by one.
int capacity()
Returns the current capacity of this vector.
void clear()
Removes all of the elements from this list.
Object clone()
Returns a shallow copy of this ArrayList instance.
boolean containsAll(Collection<?> c)
Returns true if this Vector contains all of the elements in the specified Collection.
boolean contains(Object o)
Returns true if this list contains the specified element.
void copyInto(Object[] anArray)
Copies the components of this vector into the specified array.
E elementAt(int index)
Returns the component at the specified index.
Enumeration<E> elements()
Returns an enumeration of the components of this vector.
void ensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
boolean equals(Object o)
Compares the specified Object with this Vector for equality.
E firstElement()
Returns the first component (the item at index 0) of this vector.
E get(int index)
Returns the element at the specified position in this Vector.
int hashCode()
Returns the hash code value for this Vector.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int indexOf(Object o, int index)
Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
void insertElementAt(E obj, int index)
Inserts the specified object as a component in this vector at the specified index.
boolean isEmpty()
Tests if this vector has no components.
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
E lastElement()
Returns the last component of the vector.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int lastIndexOf(Object o, int index)
Returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found.
ListIterator<E>  listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index)
Removes the element at the specified position in this Vector.
boolean remove(Object o)
Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
boolean removeAll(Collection<?> c)
Removes from this Vector all of its elements that are contained in the specified Collection.
void removeAllElements()
Removes all components from this vector and sets its size to zero.
boolean removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this vector.
void removeElementAt(int index)
Deletes the component at the specified index.
protected void removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
boolean retainAll(Collection<?> c)
Retains only the elements in this Vector that are contained in the specified Collection.
E set(int index, E element)
Replaces the element at the specified position in this Vector with the specified element.
void setElementAt(E obj, int index)
Sets the component at the specified index of this vector to be the specified object.
void setSize(int newSize)
Sets the size of this vector.
int size()
Returns the number of components in this vector.
List<E>  subList(int fromIndex, int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray()
Returns an array containing all of the elements in this Vector in the correct order.
<T> T[]  toArray(T[] a)
Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.
String toString()
Returns a string representation of this Vector, containing the String representation of each element.
void trimToSize()
Trims the capacity of this vector to be the vector's current size.
 
Example of Vector:

import java.util.Vector;

public class VectorClassExample {

public static void main(String[] args) {
Vector<String> v = new Vector<String>();

System.out.println("Initial Size of Vector :\t"+v.size());
//Adding Elements in Vector
v.add("Apple");
v.add("Banana");
v.add("Cake");
v.add("Guava");

//This method adds the element at end of vector
v.addElement("Orange");

System.out.println("Displaying Vector :\t"+v);
System.out.println("capacity() ,This method return current capacity :\t"+v.capacity());
System.out.println("clone() ,This method returns clone :\t"+v.clone());
System.out.println("contains() ,This method return true/false if element found :\t"+v.contains("Cake"));
System.out.println("elementAt(e) ,This method return element at specified index :\t"+v.elementAt(2));
System.out.println("firstElement() ,This method return first element :\t"+v.firstElement());
System.out.println("get() ,This method returns element at specified position :\t"+v.get(3));
System.out.println("hashCode() ,This method return hashcode of vector :\t"+v.hashCode());
System.out.println("indexOf() ,This method return index of specified element :\t"+v.indexOf("Orange"));
System.out.println("indexOf(e,int) ,This method returns index at provided index,if not found search forward also :\t"+v.indexOf("Orange", 1));
System.out.println("isEmpty() ,This method return true/false :\t"+v.isEmpty());
System.out.println("lastElement() ,This method return last element :\t"+v.lastElement());
System.out.println("lastIndexOf(e) ,This method return index of last occurence :\t"+v.lastIndexOf(3));
System.out.println("remove(e) ,This method remove element at specified index :\t"+v.remove(2));
System.out.println("remove(o) ,This method remove element :\t"+v.remove("Guava"));
System.out.println("set(int,o) ,This method sets & replace the elements at specified index :\t"+v.set(1, "Pankaj"));
System.out.println("subList(1,3) ,This method return elements sub list between fromIndex & toIndex :\t"+v.subList(1, 3));

//This method set the size of vector
v.setSize(6);
//This method sets the element at specified index
v.setElementAt("Pushkar", 2);
System.out.println("Displaying Vector :\t"+v);

//This method adds element at specified index
v.insertElementAt("Ram Prased", 2);
//This method removes element at specified index
v.removeElementAt(1);
//This method Remove all elements
v.clear();
//This method retains only those element which contained in specified collection 
v.retainAll(v);
//This method Removes all elements and set size to 0
v.removeAllElements();

System.out.println("Size of Vector after Elements Addition :\t"+v.size());
}

}
Program Output:

Initial Size of Vector : 0
Displaying Vector : [Apple, Banana, Cake, Guava, Orange]
capacity() ,This method return current capacity : 10
clone() ,This method returns clone : [Apple, Banana, Cake, Guava, Orange]
contains() ,This method return true/false if element found : true
elementAt(e) ,This method return element at specified index : Cake
firstElement() ,This method return first element : Apple
get() ,This method returns element at specified position : Guava
hashCode() ,This method return hashcode of vector : 2109281436
indexOf() ,This method return index of specified element : 4
indexOf(e,int) ,This method returns index at provided index,if not found search forward also : 4
isEmpty() ,This method return true/false : false
lastElement() ,This method return last element : Orange
lastIndexOf(e) ,This method return index of last occurence : -1
remove(e) ,This method remove element at specified index : Cake
remove(o) ,This method remove element : true
set(int,o) ,This method sets & replace the elements at specified index : Banana
subList(1,3) ,This method return elements sub list between fromIndex & toIndex : [Pankaj, Orange]
Displaying Vector : [Apple, Pankaj, Pushkar, null, null, null]
Size of Vector after Elements Addition : 0



Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment