2020
2121### Lists
2222
23- - ** ArrayList**
24- + Backed by array (which are co-located in memory), thus fast iteration and get(i) operation.
25- + Slow inserts when the backed array is full and has to double in size.
26- + Fail-fast iterators, which can throw ConcurrentModificationException.
27- + Add is O(n) - When element is added to middle of list, all elements on the right have to be moved.
28- + [ Use Case] ( http://stackoverflow.com/a/322742/3494368 ) - When iterations outnumber number of read/writes.
23+ #### ArrayList
24+
25+ - Backed by array (which are co-located in memory), thus fast iteration and get(i) operation.
26+ - Slow inserts when the backed array is full and has to double in size.
27+ - Fail-fast iterators, which can throw ConcurrentModificationException.
28+ - Add is O(n) - When element is added to middle of list, all elements on the right have to be moved.
29+ - [ Use Case] ( http://stackoverflow.com/a/322742/3494368 ) - When iterations outnumber number of read/writes.
2930
30- - ** LinkedList**
31- + Chain of nodes referencing each other (doubly linked list).
32- + No co-location of nodes, pointers need to be chased for next element, thus slow iterations and get(i) operation.
33- + Fail-fast iterators, which can throw ConcurrentModificationException.
34- + Implements Queue interface, thus allows offer/pop/peek operations.
35- + Add is O(1) - Adding element in middle of list is just adjusting the node pointers.
36- + Internally uses references (~ to skiplist) to optimize iterations.
37- + [ Use Case] ( http://stackoverflow.com/a/322742/3494368 ) - Lot of inserts in middle of the list.
31+ #### LinkedList
32+
33+ - Chain of nodes referencing each other (doubly linked list).
34+ - No co-location of nodes, pointers need to be chased for next element, thus slow iterations and get(i) operation.
35+ - Fail-fast iterators, which can throw ConcurrentModificationException.
36+ - Implements Queue interface, thus allows offer/pop/peek operations.
37+ - Add is O(1) - Adding element in middle of list is just adjusting the node pointers.
38+ - Internally uses references (~ to skiplist) to optimize iterations.
39+ - [ Use Case] ( http://stackoverflow.com/a/322742/3494368 ) - Lot of inserts in middle of the list.
3840
3941| Operation | ArrayList | LinkedList |
4042| -----------------| -----------------------------------| ---------------------------|
4345| remove(i) | O(n) Remove and move all elements | O(n) Iterate then remove |
4446| iterator.remove | O(n) | O(1) |
4547
46- - ** Stack**
47- + For stack operations push/pop/peek.
48- + Not used anymore. Recommended to use Deque implementations.
48+ #### Stack
49+
50+ - For stack operations push/pop/peek.
51+ - Not used anymore. Recommended to use Deque implementations.
52+
53+ #### Vector
4954
50- - ** Vector**
51- + Synchronized version of list.
52- + Not used anymore. Recommended below mentioned alternatives.
55+ - Synchronized version of list.
56+ - Not used anymore. Recommended below mentioned alternatives.
5357
54- - ** CopyOnWriteArrayList**
55- + Thread-safe.
56- + Backed array is copied during every element insert.
57- + Avoids ConcurrentModificationException since iteration can continue in original copy, and insert results in new copy.
58- + High memory usage (more pressure on GC) due to the resulting copies.
59- + Use case - Large number of threads for read, low number of writes.
58+ #### CopyOnWriteArrayList
59+
60+ - Thread-safe.
61+ - Backed array is copied during every element insert.
62+ - Avoids ConcurrentModificationException since iteration can continue in original copy, and insert results in new copy.
63+ - High memory usage (more pressure on GC) due to the resulting copies.
64+ - Use case - Large number of threads for read, low number of writes.
6065
61- - ** Collections.synchronizedList**
62- + Thread-safe.
63- + Can be slow due to mutual exclusion.
64- + Iterations have to be externally synchronized by developer
65- + Can throw ConcurrentModificationException if (above mentioned) synchronization not done during iteration.
66+ #### Collections.synchronizedList
67+
68+ - Thread-safe.
69+ - Can be slow due to mutual exclusion.
70+ - Iterations have to be externally synchronized by developer
71+ - Can throw ConcurrentModificationException if (above mentioned) synchronization not done during iteration.
6672
6773### Sets
6874
6975Collection of unique elements. No duplicates.
7076
71- - ** HashSet**
72- + Backed by HashMap.
73- + Performance can vary based on hashCode implementation.
74- + Constant time get/remove/add/contains (subject to above point).
75- + Fail-fast iterators.
76- + Insertion order not retained.
77+ #### HashSet
78+
79+ - Backed by HashMap.
80+ - Performance can vary based on hashCode implementation.
81+ - Constant time get/remove/add/contains (subject to above point).
82+ - Fail-fast iterators.
83+ - Insertion order not retained.
7784
78- - ** LinkedHashSet**
79- + Insertion order is retained.
80- + Uses doubly-linked list to maintain the order.
81- + Iteration can be slower due to this.
82- + Other features, same as HashSet above (except iteration)
83-
84- - ** TreeSet**
85- + Elements sorted by their natural order (or Comparator passed in constructor).
86- + Log(n) time for add/remove/contains operations.
87- + Navigable (floor, ceiling, higher, lower, headSet, tailSet operations).
88- + Fail fast iterators.
89-
90- - ** ConcurrentSkipListSet**
91- + Thread-safe.
92- + Log(n) time for add/remove/contains operations.
93- + Navigable (floor, ceiling, higher, lower, headSet, tailSet operations).
94- + Size method is not constant time operation.
95- + Weakly consistent iterators (do not throw ConcurrentModificationException but also __ may not__ reflect concurrently added items).
96- + Thus, bulk operations (addAll, removeAll, retainAll, containsAll etc) are not guaranteed to be atomic.
97-
98- - ** CopyOnWriteArraySet**
99- + Backed by CopyOnWriteArrayList
100- + Thread-safe.
101- + Slow. Operations have to iterate through the array for most operations.
102- + Recommended where reads vastly outnumber writes and set size is small.
85+ #### LinkedHashSet
86+
87+ - Insertion order is retained.
88+ - Uses doubly-linked list to maintain the order.
89+ - Iteration can be slower due to this.
90+ - Other features, same as HashSet above (except iteration)
91+
92+ #### TreeSet
93+
94+ - Elements sorted by their natural order (or Comparator passed in constructor).
95+ - Log(n) time for add/remove/contains operations.
96+ - Navigable (floor, ceiling, higher, lower, headSet, tailSet operations).
97+ - Fail fast iterators.
98+
99+ #### ConcurrentSkipListSet
100+
101+ - Thread-safe.
102+ - Log(n) time for add/remove/contains operations.
103+ - Navigable (floor, ceiling, higher, lower, headSet, tailSet operations).
104+ - Size method is not constant time operation.
105+ - Weakly consistent iterators (do not throw ConcurrentModificationException but also __ may not__ reflect concurrently added items).
106+ - Thus, bulk operations (addAll, removeAll, retainAll, containsAll etc) are not guaranteed to be atomic.
107+
108+ #### CopyOnWriteArraySet
109+
110+ - Backed by CopyOnWriteArrayList
111+ - Thread-safe.
112+ - Slow. Operations have to iterate through the array for most operations.
113+ - Recommended where reads vastly outnumber writes and set size is small.
103114
104- - ** EnumSet**
105- + To be used with Enum types.
106- + Very efficient and fast (backed by bit-vectors).
107- + Weakly consistent iterators.
108- + Nulls not allowed.
115+ #### EnumSet
116+
117+ - To be used with Enum types.
118+ - Very efficient and fast (backed by bit-vectors).
119+ - Weakly consistent iterators.
120+ - Nulls not allowed.
109121
110122### Maps
111123
112- - ** HashMap**
113- + key, value pairs.
114- + Permits a null key, and null values.
115- + Iteration order not guaranteed.
116- + Throws ConcurrentModificationException.
117- + [ Article detailing implementation] ( http://www.deepakvadgama.com/blog/java-hashmap-internals/ ) .
118-
119- - ** HashMap implementation details**
120- + Backed by array (buckets), array-size is known as table-size.
121- + Position in array = element-hash % table-size.
122- + If elements end up in same bucket, they are added to linked-list (or a balanced red-black tree).
123- + O(1) access (if hashcode properly distributes the values, else O(n) for linked-list & O(log(n)) for tree.
124- + Load factor - 0.75 default, decides when table-size should increase (double).
125- + Bigger load-factor - more space-efficient, reduced speed (due to more elements in same bucket).
126- + Lower load-factor - less space-efficient, more speed (less, ideally 1 element in 1 bucket).
127- + Initial table-size = 16.
128-
129- - ** LinkedHashMap**
130- + Insertion order is retained.
131-
132- - ** Hashtable**
133- + Thread-safe.
134- + Not used anymore, ConcurrentHashMap recommended.
135-
136- - ** ConcurrentHashMap**
137- + Thread-safe.
138- + Fine grained locking called striped locking (map is divided into segments, each with associated lock. Threads holding different locks don't conflict).
139- + Improved performance over Hashtable.
140-
141- - ** TreeMap**
142- + Sorted by keys.
143- + Uses Red-Black tree implementation.
144-
145- - ** ConcurrentSkipListMap**
146- + Thread-safe version of TreeMap.
147- + Navigable (floor, ceiling, higher, lower, headSet, tailSet operations).
124+ #### HashMap
125+
126+ - key, value pairs.
127+ - Permits a null key, and null values.
128+ - Iteration order not guaranteed.
129+ - Throws ConcurrentModificationException.
130+ - [ Article detailing implementation] ( http://www.deepakvadgama.com/blog/java-hashmap-internals/ ) .
131+
132+ #### HashMap implementation details
133+
134+ - Backed by array (buckets), array-size is known as table-size.
135+ - Position in array = element-hash % table-size.
136+ - If elements end up in same bucket, they are added to linked-list (or a balanced red-black tree).
137+ - O(1) access (if hashcode properly distributes the values, else O(n) for linked-list & O(log(n)) for tree.
138+ - Load factor - 0.75 default, decides when table-size should increase (double).
139+ - Bigger load-factor - more space-efficient, reduced speed (due to more elements in same bucket).
140+ - Lower load-factor - less space-efficient, more speed (less, ideally 1 element in 1 bucket).
141+ - Initial table-size = 16.
142+
143+ #### LinkedHashMap
144+
145+ - Insertion order is retained.
146+
147+ #### Hashtable
148+
149+ - Thread-safe.
150+ - Not used anymore, ConcurrentHashMap recommended.
151+
152+ #### ConcurrentHashMap
153+
154+ - Thread-safe.
155+ - Fine grained locking called striped locking (map is divided into segments, each with associated lock. Threads holding different locks don't conflict).
156+ - Improved performance over Hashtable.
157+
158+ #### TreeMap
159+
160+ - Sorted by keys.
161+ - Uses Red-Black tree implementation.
162+
163+ #### ConcurrentSkipListMap
164+
165+ - Thread-safe version of TreeMap.
166+ - Navigable (floor, ceiling, higher, lower, headSet, tailSet operations).
148167
149168### Queues
150169
151- - ** LinkedList**
152- + Implements Queue interface.
153- + offer, peek, poll operations.
154- + Use case - task queues
155-
156- - ** ArrayBlockingQueue**
157- + Thread-safe.
158- + Backed by array. Thus bounded in size.
159- + Adding element to full queue results in blocking.
160- + Polling an empty queue results in blocking.
161- + Use case - Producer consumer problem.
170+ #### LinkedList
171+
172+ - Implements Queue interface.
173+ - offer, peek, poll operations.
174+ - Use case - task queues
175+
176+ #### ArrayBlockingQueue
177+
178+ - Thread-safe.
179+ - Backed by array. Thus bounded in size.
180+ - Adding element to full queue results in blocking.
181+ - Polling an empty queue results in blocking.
182+ - Use case - Producer consumer problem.
162183
163- - ** LinkedBlockingQueue**
164- + Thread-safe.
165- + Backed by linked-list.
166- + Optionally bounded in size. Takes maxSize as constructor argument.
167-
168- - ** ConcurrentLinkedQueue**
169- + Thread-safe.
170- + Uses CAS (Compare-And-Swap) for more throughput. Also known as lock free.
171-
172- - ** Deque classes**
173- + ArrayDeque - Double ended queue. Backed by array. Can throw ConcurrentModificationException.
174- + LinkedList - Implements Deque interface.
175- + LinkedBlockingDeque
176- + ConcurrentLinkedDeque
177-
178- - ** PriorityQueue**
179- + Elements sorted based on their natural order (or Comparator provided in Constructor).
180- + Use case - task queues where tasks can have different priorities.
184+ #### LinkedBlockingQueue
185+
186+ - Thread-safe.
187+ - Backed by linked-list.
188+ - Optionally bounded in size. Takes maxSize as constructor argument.
189+
190+ #### ConcurrentLinkedQueue
191+
192+ - Thread-safe.
193+ - Uses CAS (Compare-And-Swap) for more throughput. Also known as lock free.
194+
195+ #### Deque classes
196+
197+ - ArrayDeque - Double ended queue. Backed by array. Can throw ConcurrentModificationException.
198+ - LinkedList - Implements Deque interface.
199+ - LinkedBlockingDeque
200+ - ConcurrentLinkedDeque
201+
202+ #### PriorityQueue
203+
204+ - Elements sorted based on their natural order (or Comparator provided in Constructor).
205+ - Use case - task queues where tasks can have different priorities.
181206
182- - ** PriorityBlockingQueue**
183- + Thread-safe.
207+ #### PriorityBlockingQueue
208+
209+ - Thread-safe.
184210
185- - ** DelayQueue**
186- + Elements added, are available to be removed only after their delay-time is expired.
211+ #### DelayQueue
212+
213+ - Elements added, are available to be removed only after their delay-time is expired.
214+
215+ #### SynchronousQueue
187216
188- - ** SynchronousQueue**
189- + Holds single elements.
190- + Blocks for both producer and consumer to arrive.
191- + Use case - For safe/atomic transfer of objects between threads.
217+ - Holds single elements.
218+ - Blocks for both producer and consumer to arrive.
219+ - Use case - For safe/atomic transfer of objects between threads.
192220
193221### equals and hashCode
194222
@@ -197,7 +225,8 @@ Collection of unique elements. No duplicates.
197225
198226### Collections class
199227
200- ** Utility methods**
228+ #### Utility methods
229+
201230
202231- sort(list, key) - guarantees stable sort
203232- reverse
@@ -209,7 +238,8 @@ Collection of unique elements. No duplicates.
209238 + log(n) if list implements RandomAccess, else O(n)
210239 + RandomAccess - Marker interface that says, collection supports fast random access, get(i). Typically backed by arrays.
211240
212- ** Methods returning wrapped instances**
241+ #### Methods returning wrapped instances
242+
213243
214244- empty - emptyList, emptySet, emptyMap etc.
215245- synchronized - synchronizedList, synchronizedSet, synchronizedMap etc.
0 commit comments