Sunday, November 6, 2016

ArrayList vs Vector



ArrayList
Vector
ArrayList & Vector
Maintains the insertion order of element
Maintains the insertion order of element
Both are ordered collection classes as they maintain the elements insertion order.
ArrayList is not Synchronized.

We can make this Synchronized.
[Collections.synchronizedList(new ArrayList());]
Vector is Synchronized.
ArrayList  is not thread-safe

Vector is Thread-safe, shared between multiple threads.
Faster than Vector.
Give Better performance than Vector.
Better for non-threaded applications.
Much Slower than ArryaList for single thread use.
(Synchronization makes Vector slow)


ArrayList and Vector allows null and duplicates.
ArrayList is using Iterator for Traversing.
Iterator in the ArrayList is fail-fast iterator.
Vector is using both Iterator and Enumerator for Traversing.

Enumerator in the Vector is not fail-fast.
Both Iterator and ListIterator returned by ArrayList and Vector are fail_fast
ArrayList grow by half of its size when resized.
It increase its array size by 50% while insertion.
Vector doubles the size of its array size.
Vector increments 100% if it exceeds its capacity.

Both use dynamically resizable array as a data structure internally
They both can grow and shrink dynamically .(increase or decrease size) when overflow and deletion happens

Doesn’t define increment size.
Default size is 10. (If you create an arraylist means , you had created array with 10 elements and with values for all as null)


It is better to set initial capacity if you know the size in advance.

ArrayList<?> list = new ArrayList<>(20);
 So that we can avoid the cost of resizing.(otherwise if it cross size 10,it will create a new array and copy the data of old to new and , GC the old one)
We can set increment size for vector.
public synchronized void setSize(int i) {}
If you don’t know initial capacity , but if u know the rate at which array can grow use vector.

Introduced in java version 1.2(with collections).
Not a Legacy Class
Introduced in first Versions of Java.
As part of Legacy Classes (older version of java, not used by any one)




Difference between Collection and Collections


collection
collections
Collection & collections
Interface
Root interface for java collection Framework(all the collection interface (List,Set,Queue) are sub interface of it.
Class
Class is a member of Java collection framework.
Interfaces and its implementations classes together form collection framework.
Map interface, doesn’t inherit from Collection interface
Member of java.util package.
Contains abstract methods.

public interface Collection<E>
extends Iterable<E>

Utility class of java.util package.
Contains static method to operate on object of type collection.
public class Collections extends Object
Both are from java.util package.
ArrayList is a class that , implements List interface(sub interface of Collection Interfae)
e.g.
ArrayList<Integer> arrList = new ArrayList<Integer>()
Collections class provide static utility methdos to operate on the object of ArryaList.
e.g. Collections.sort(arrList)

Collection ß  Set,List,Queue

Set ß SortedSetß NavigableSet
Queueß Deque
public class Collections extends Object
Contains methods to perform basic operations.

int size()
boolean isEmpty()               
boolean contains(Object o)                
Iterator<E> iterator()            
Object[] toArray()  
<T> T[] toArray(T[] a)           
boolean add(E e)
boolean remove(Object o)
boolean containsAll(Collection<?> c)
boolean addAll(Collection<? extends E> c)
boolean removeAll(Collection<?> c)             
boolean retainAll(Collection<?> c)                
void clear()            
boolean equals(Object o)
int hashCode()
Stream<E> stream() -> java 8 and later
Stream<E> parallelStream()->java 8 and later

iterator is the only one inherited form Iterator interface.

Some methods from Collections class, to perform operation on collection object.

Collections.max()               
Collections.min()
Collections.sort()
Collections.shuffle()
Collections.synchronizedCollection()               
Collections.binarySearch()               
Collections.disjoint()          
Collections.copy()              
Collections.reverse()