Java Vector (List Interface)
Vector Is part of collection framework and it is available
in “java.util” package.
In Java, both Array List and Vector implements the List
interface and provides the same functionalities. However, there exist some
differences between them.
The Vector class synchronizes each individual operation.
This means whenever we want to perform some operation on
vectors, the Vector class automatically applies a lock to that operation,
because when one thread is accessing a vector, and at the same time another
thread tries to access it, an exception called Concurrent Modification
Exception is generated.
Hence, this continuous use of lock for each operation makes
vectors less efficient.
However, in Array List methods are not synchronized. Instead,
it uses the Collections.synchronizedList() method that synchronizes the list.
Array List saves memory compared with Vector. because Array List
& Vector default capacity is 10.
Array List is resized by 50% when it exceeds current default size. But Vector increase the capacity by 100%.
Creation of Vector:
Syntax:
List<E> objRef= new Vector<E>();
List<E> objRef= new Vector<E>(int
initialcapacity);
Here, E represents element data type
Methods of vector:
Some of the important methods in vector
boolean add(Element obj)
void add(int position, Element obj)
boolean remove(Element obj)
Element remove(int position)
void clear()
int size()
boolean contains(Element obj)
Element get(int position)
Element get(int position, Elemnet obj)
Java Code:
No comments:
Post a Comment