Thursday, July 23, 2020

Java Vector

Vector in java:
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Direct Known Subclasses:
Stack
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
  • Vector is a legacy class.It is introduced in JDK 1.0
  • But Vector belongs to Java Collection framework since Java 1.2.
  • Vector extends AbstractList and implements List interfaces.
  • Vector implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess interfaces.
  • Directly known subclass of Vector is Stack.
  • Vectors fall in legacy classes.
  • Vector belongs to the java.util package and implements the List interface, so we can use all the methods of List interface
  • Vector is resizable-array implementations of the List interface. 
  • Vector  internally use an array data structure to store the elements.
  • Vector is synchronized.
  • Vector is thread-safe and It is recommended to use in multithreaded environment only because it is slower then ArrayList.
  • Vector can use both Iterator interface and Enumeration interface to traverse the elements.
  • The iterators returned by this class's iterator and listIterator methods are fail-fast.
  • Java Vector contains many legacy methods that are not the part of a collections framework.
  • Vector maintains an insertion order like an ArrayList.
  • Vector has poor performance in adding, searching, deleting, and updating its elements due to it is synchronized.
  • Vector permits null values and duplicates.
  • Vector increments 100% of its initial capacity. if  the total number of elements exceeds than its     capacity.
Initial Capacity:10.
Load Factor:1 (when the list of vector is full)
Growth Rate: Groth Rate of vector is 100% that means.

Vector Example:
package com.javaiq.in;

import java.io.*;
import java.util.*;

public class VectorExample {

	public static void main(String[] arg) {
		// Creating a default vector
		Vector v1 = new Vector();
		v1.add(8);
		v1.add(6);
		v1.add("Test");
		v1.add("JavaIQ");
		v1.add(7);

		// Display the vector elements
		System.out.println("Vector v1 object elements :  " + v1);

		// Creating generic vector
		Vector<Integer> v2 = new Vector<Integer>();
		v2.add(11);
		v2.add(22);
		v2.add(33);

		// Display the vector elements
		System.out.println("Vector v2 object elements :  " + v2);
	}
}


No comments:

Post a Comment