logo

Java collection types comparison notes

Posted on
Authors

My notes about Java collection types comparison.

ArrayList vs. LinkedList

#ArrayListLinkedList
BehaviorImplement List interface, so it can act as a list.Implement List and Dequeue interface, so it can act as a list and queue both.
StoringData are stored in a dynamic array.Data are stored in a doubly-linked list.
ManipulationSlow because it internally uses an array. If any element is removed from the array, all the bits re shifted in the memory.Faster because it uses a doubly-linked list, so no bit shifting is required in the memory, only the reference link is changed.
When to use?ArrayList is better for storing and accessing data.LinkedList is better for manipulating data.

ArrayList vs. Vector

#ArrayListVector
SynchronizationNon-synchronized, which means that multiple threads can operate on ArrayList at the same time.Synchronized, which means that only one thread can access the code at a time.
LegacyArrayList is not legacy class, it was introduced in JDK 1.2.Vector is legacy class.
SizeArrayList increments 50% of its current size if element added exceeds its capacity.Vector increments 100% of its current size if element added exceeds its capacity.
SpeedArrayList is fast because it is non-synchronized.Vector is slow because it is synchronized, i.e., in a multithreading environment, it holds the other threads in runnable or non-runnable state until current thread releases the lock of the object.
IterationArrayList uses Iterator interface to traverse through elements.Vector can use both Iterator or Enumerator interface to traverse through elements.
When to use?ArrayList is use mostly in single-thread applications.Collections.synchronizedList() is preferred to Vector nowadays.

List vs. Set

#ListSet
OrderOrder sequenceUnorder sequence
DuplicationAllowedDisallowed
Null elementIt is possible to store several null elements.A null element can only be stored once.
AccessingElements can be accessed based on their position.Access to items from a certain position is not permitted.
When to use?Use it when you want to frequently access the elements by using the index.Set is used when you want to design a collection of distinct elements.

Set vs. Map

#SetMap
DuplicationDuplicate values are not permittedUnique key, but repeatable values
TraversingUse Using the keyset() and entryset() methodsNot possible, you must convert Map to Set
Insertion OrderBoth Set and Map don’t keep the insertion order, however, some of Set’s classes, such as LinkedHashSet, keep the insertion order.

HashMap vs. TreeMap

#HashMapTreeMap
ImplementationThe Java HashMap implementation of the Map interface is based on hash tables.Java TreeMap is a Map interface implementation based on a Red-Black Tree structure, which is a self balancing binary search tree.
InterfaceThe Map, Cloneable, and Serializable interfaces are implemented by HashMap.NavigableMap, Cloneable, and Serializable interfaces are implemented by TreeMap.
Null Keys/ Valuesnull key: allow only one null key, null value: allow multiple.null key: not permitted, null value: allow multiple.
Data TypesHashMap does not perform sorting on keys, so it allows for heterogeneous elements.TreeMap allows homogeneous values as a key because of sorting.
PerformanceHashMap is faster than TreeMap because it provides constant time performance that is O(1) for the basic operation like get() and put()TreeMap is slow in comparison to HaskMap because it provides the performance of O(log(n) for most operation like add(), remove(), and contains().
Order of elementsHashMap does not maintains any order.TreeMap elements are sorted in natural ordering (ascending).
When to use?Use HashMap when we does not require key-value pair in sorted orderUse TreeMap when we require key-value pair in ascending order.