- Comparison
- Time Complexity
ArrayList
Implements List -> Collection
Insertion order preserved, Allows: Duplicates, Heterogeneous objects & null
Default capacity or size is 10, which subsequently increases with formula as
New Capacity = (Current Capacity * 1.5)+1
Constructors:
Default: Creates with size = 10
int initialCapacity = Creates with predefined size = initialCapacity
Collections C = convert any collection to ArrayList
Highly recommended when there are frequent retrieval operations
Not recommended when there are frequent random inserts or deletes as it needs rearranging the indexes
To get synchronized ArrayList following can be done
List nonSyncList = new ArrayList();
List syncList = Collections.synchronizedList(nonSyncList);
Similarly, Collections class also provides synchronizedSet & synchronizedMap methods, to get a synchronized Set or Map
LinkedList
Implements List -> Collection
Insertion order preserved, Allows: Duplicates, Heterogeneous objects & null
By default LinkedList creates Empty list, memory allocation is done at runtime
Constructors
Default
Collections C = convert any collection to LinkedList
Highly recommended when there are frequent random insert or deletes
Not recommended when there are frequent retrieval operations as it needs rearranging the indexes
LinkedList provides a few methods like addFirst, addLast, removeFirst, removeLast, getFirst & getLast to support Stack & Queue based implementation using LinkedList
Version 1.5 onwards LinkedList also implements Queue interface, such LinkedList implementation will always follow FIFO order
Vector
Implements List -> Collection
It is similar to ArrayList, except Vector is synchronized i.e. Thread safe
Insertion order preserved, Allows: Duplicates, Heterogeneous objects & null
Default capacity or size is 10, which subsequently doubles
So ArrayList is faster than Vector
Constructors
Default
int initialCapacity = Creates with predefined size = initialCapacity
int initialCapacity, int incrementalCapacity = Creates with predefined size = initialCapacity and grows with incrementalCapacity (instead of double)
Collections C = convert any collection to Vector
CopyOnWriteArrayList
ThreadSafe version of ArrayList
Any number of threads can perform read operations
For update/write a clone (copy) of List will be created in which write/updates operations will be performed
JVM will internally take care of synching these clone copies with list
NOT recommended when there are large number of write/update operations
Iterator of CopyOnWriteArraList cannot remove the objects, else UnsupportedOperationException
Constructors
Default
Collection c
Object []
Methods
All methods from List(Interface) & Collection (Interface), in addition
boolean addIfAbsent(Object)
int addAllAbsent(Collection)
CopyOnWriteArraySet
Underlying implementation is CopyOnWriteArrayList
Same as CopyOnWriteArrayList but duplicates are NOT allowed
Constructors
Default
Collections
Methods
No new methods, ONLY from Set & Collection interface
No comments:
Post a Comment