|
1 | 1 | package com.in28minutes.java.collections;
|
2 | 2 |
|
| 3 | +import java.util.Comparator; |
3 | 4 | import java.util.Iterator;
|
4 | 5 | import java.util.ListIterator;
|
| 6 | +import java.util.concurrent.TimeUnit; |
5 | 7 |
|
6 | 8 | //Java COLLECTION INTERVIEW QUESTIONS
|
7 | 9 | //�����������������������������������
|
@@ -60,39 +62,14 @@ interface Collection<E> extends Iterable<E> {
|
60 | 62 | int hashCode();
|
61 | 63 | }
|
62 | 64 |
|
63 |
| -// Unique things only - Does not allow duplication. |
64 |
| -// If obj1.equals(obj2) then only one of them can be in the Set. |
65 |
| -interface Set<E> extends Collection<E> { |
66 |
| - |
67 |
| -} |
68 |
| - |
69 |
| -// A, X , B |
70 |
| -// X, A ,B |
71 |
| -class HashSet /* implements Set */{ |
72 |
| - // unordered, unsorted - iterates in random order |
73 |
| - // uses hashCode() |
74 |
| -} |
75 |
| - |
76 |
| -// A, X, B |
77 |
| -// A, X, B |
78 |
| -class LinkedHashSet /* implements Set */{ |
79 |
| - // ordered - iterates in order of insertion |
80 |
| - // unsorted |
81 |
| - // uses hashCode() |
82 |
| -} |
83 |
| - |
84 |
| -// A,C,B |
85 |
| -// A,B,C |
86 |
| -class TreeSet /* implements Set,NavigableSet */{ |
87 |
| - // 3,5,7 |
88 |
| - // sorted - natural order |
89 |
| - // implements NavigableSet |
90 |
| -} |
| 65 | +//����������������������������������� |
| 66 | +//LIST, RELATED INTERFACES AND CLASSES |
| 67 | +//����������������������������������� |
91 | 68 |
|
92 |
| -// LIST OF THINGS |
93 |
| -// Cares about which position each object is in |
94 |
| -// Elements can be added in by specifying position - where should it be added in |
95 |
| -// If element is added without specifying position - it is added at the end |
| 69 | +//LIST OF THINGS |
| 70 | +//Cares about which position each object is in |
| 71 | +//Elements can be added in by specifying position - where should it be added in |
| 72 | +//If element is added without specifying position - it is added at the end |
96 | 73 | interface List<E> extends Collection<E> {
|
97 | 74 |
|
98 | 75 | // [1,2,3,4]
|
@@ -144,28 +121,189 @@ class LinkedList /* implements List<E>, Queue */{
|
144 | 121 | // and remove()
|
145 | 122 | }
|
146 | 123 |
|
| 124 | +//����������������������������������� |
| 125 | +//SET RELATED INTERFACES AND CLASSES |
| 126 | +//����������������������������������� |
| 127 | + |
| 128 | +// Unique things only - Does not allow duplication. |
| 129 | +// If obj1.equals(obj2) then only one of them can be in the Set. |
| 130 | +interface Set<E> extends Collection<E> { |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +//Main difference between Set and SortedSet is - an implementation of SortedSet interface |
| 135 | +//maintains its elements in a sorted order. Set interface does not guarantee any Order. |
| 136 | +interface SortedSet<E> extends Set<E> { |
| 137 | + |
| 138 | + SortedSet<E> subSet(E fromElement, E toElement); |
| 139 | + |
| 140 | + SortedSet<E> headSet(E toElement); |
| 141 | + |
| 142 | + SortedSet<E> tailSet(E fromElement); |
| 143 | + |
| 144 | + E first(); |
| 145 | + |
| 146 | + E last(); |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | +//A SortedSet extended with navigation methods reporting closest matches for given search targets. |
| 151 | +interface NavigableSet<E> extends SortedSet<E> { |
| 152 | + E lower(E e); |
| 153 | + |
| 154 | + E floor(E e); |
| 155 | + |
| 156 | + E ceiling(E e); |
| 157 | + |
| 158 | + E higher(E e); |
| 159 | + |
| 160 | + E pollFirst(); |
| 161 | + |
| 162 | + E pollLast(); |
| 163 | +} |
| 164 | + |
| 165 | +// Order of Insertion : A, X , B |
| 166 | +// Possible Order of Storing : X, A ,B |
| 167 | +class HashSet /* implements Set */{ |
| 168 | + // unordered, unsorted - iterates in random order |
| 169 | + // uses hashCode() |
| 170 | +} |
| 171 | + |
| 172 | +// Order of Insertion :A, X, B |
| 173 | +// Order of Storing : A, X, B |
| 174 | +class LinkedHashSet /* implements Set */{ |
| 175 | + // ordered - iterates in order of insertion |
| 176 | + // unsorted |
| 177 | + // uses hashCode() |
| 178 | +} |
| 179 | + |
| 180 | +// Order of Insertion :A,C,B |
| 181 | +// Order of Storing : A,B,C |
| 182 | +class TreeSet /* implements Set,NavigableSet */{ |
| 183 | + // 3,5,7 |
| 184 | + // sorted - natural order |
| 185 | + // implements NavigableSet |
| 186 | +} |
| 187 | + |
| 188 | +//����������������������������������� |
| 189 | +//QUEUE RELATED INTERFACES AND CLASSES |
| 190 | +//����������������������������������� |
147 | 191 | // Arranged in order of processing - A to-do list for example
|
148 | 192 | // Queue interface extends Collection. So, it supports all Collection Methods.
|
149 | 193 | interface Queue<E> extends Collection<E> {
|
| 194 | + |
| 195 | + //Inserts the specified element into this queue |
| 196 | + //Throws exception in case of failure |
150 | 197 | boolean add(E paramE);
|
151 | 198 |
|
| 199 | + //Inserts the specified element into this queue |
| 200 | + //Returns false in case of failure |
152 | 201 | boolean offer(E paramE);
|
153 | 202 |
|
| 203 | + //Retrieves and removes the head of this queue. |
| 204 | + //Throws Exception if Queue is empty |
154 | 205 | E remove();
|
155 | 206 |
|
| 207 | + //Retrieves and removes the head of this queue. |
| 208 | + //returns null if Queue is empty |
156 | 209 | E poll();
|
157 | 210 |
|
158 | 211 | E element();
|
159 | 212 |
|
160 | 213 | E peek();
|
161 | 214 | }
|
162 | 215 |
|
| 216 | +//A linear collection that supports element insertion and removal at both ends |
| 217 | +interface Deque<E> extends Queue<E> { |
| 218 | + void addFirst(E e); |
| 219 | + |
| 220 | + void addLast(E e); |
| 221 | + |
| 222 | + boolean offerFirst(E e); |
| 223 | + |
| 224 | + boolean offerLast(E e); |
| 225 | + |
| 226 | + E removeFirst(); |
| 227 | + |
| 228 | + E removeLast(); |
| 229 | + |
| 230 | + E pollFirst(); |
| 231 | + |
| 232 | + E pollLast(); |
| 233 | + |
| 234 | + E getFirst(); |
| 235 | + |
| 236 | + E getLast(); |
| 237 | + |
| 238 | + E peekFirst(); |
| 239 | + |
| 240 | + E peekLast(); |
| 241 | + |
| 242 | + boolean removeFirstOccurrence(Object o); |
| 243 | + |
| 244 | + boolean removeLastOccurrence(Object o); |
| 245 | + |
| 246 | +} |
| 247 | + |
| 248 | +//A Queue that additionally supports operations that wait for |
| 249 | +//the queue to become non-empty when retrieving an |
| 250 | +//element, and wait for space to become available in the queue when |
| 251 | +//storing an element. |
| 252 | +interface BlockingQueue<E> extends Queue<E> { |
| 253 | + //Same as in Queue Interface |
| 254 | + //Inserts the specified element into queue IMMEDIATELY |
| 255 | + //Throws exception in case of failure |
| 256 | + boolean add(E e); |
| 257 | + |
| 258 | + //Same as in Queue Interface |
| 259 | + //Inserts the specified element into queue IMMEDIATELY |
| 260 | + //Returns false in case of failure |
| 261 | + boolean offer(E e); //Same as in Queue Interface |
| 262 | + |
| 263 | + //Inserts the specified element into this queue, waiting |
| 264 | + //if necessary for space to become available. |
| 265 | + void put(E e) throws InterruptedException; |
| 266 | + |
| 267 | + //waiting up to the specified wait time |
| 268 | + boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; |
| 269 | + |
| 270 | + //waits until element becomes available |
| 271 | + E take() throws InterruptedException; |
| 272 | + |
| 273 | + //waits for specified time and returns null if time expires |
| 274 | + E poll(long timeout, TimeUnit unit) throws InterruptedException; |
| 275 | + |
| 276 | + int remainingCapacity(); |
| 277 | + |
| 278 | + boolean remove(Object o); |
| 279 | + |
| 280 | + public boolean contains(Object o); |
| 281 | + |
| 282 | + int drainTo(Collection<? super E> c); |
| 283 | + |
| 284 | + int drainTo(Collection<? super E> c, int maxElements); |
| 285 | +} |
| 286 | + |
| 287 | +//The elements of the priority queue are ordered according to their natural ordering |
163 | 288 | class PriorityQueue /* implements Queue */{
|
164 | 289 | // sorted - natural order
|
165 |
| - // |
166 | 290 |
|
167 | 291 | }
|
168 | 292 |
|
| 293 | +class ArrayDeque /*implements Deque*/{ |
| 294 | + |
| 295 | +} |
| 296 | + |
| 297 | +class ArrayBlockingQueue /*implements BlockingQueue*/{ |
| 298 | + //uses Array - optionally-bounded |
| 299 | +} |
| 300 | + |
| 301 | +class LinkedBlockingQueue /*implements BlockingQueue*/{ |
| 302 | + //uses Linked List - optionally-bounded |
| 303 | + //Linked queues typically have higher throughput than array-based queues but |
| 304 | + //less predictable performance in most concurrent applications. |
| 305 | +} |
| 306 | + |
169 | 307 | // A,C,A,C,E,C,M,D,H,A
|
170 | 308 |
|
171 | 309 | // {("A",5),("C",2)}
|
@@ -218,6 +356,55 @@ public static abstract interface Entry<K, V> {
|
218 | 356 | }
|
219 | 357 | }
|
220 | 358 |
|
| 359 | +// A Map that orders based on the keys. Comparator can be provided at |
| 360 | +// map creation time |
| 361 | +interface SortedMap<K, V> extends Map<K, V> { |
| 362 | + Comparator<? super K> comparator(); |
| 363 | + |
| 364 | + SortedMap<K, V> subMap(K fromKey, K toKey); |
| 365 | + |
| 366 | + SortedMap<K, V> headMap(K toKey); |
| 367 | + |
| 368 | + SortedMap<K, V> tailMap(K fromKey); |
| 369 | + |
| 370 | + K firstKey(); |
| 371 | + |
| 372 | + K lastKey(); |
| 373 | +} |
| 374 | + |
| 375 | +//A SortedMap extended with navigation methods reporting closest matches for given search targets. |
| 376 | +interface NavigableMap<K, V> extends SortedMap<K, V> { |
| 377 | + Map.Entry<K, V> lowerEntry(K key); |
| 378 | + |
| 379 | + K lowerKey(K key); |
| 380 | + |
| 381 | + Map.Entry<K, V> floorEntry(K key); |
| 382 | + |
| 383 | + K floorKey(K key); |
| 384 | + |
| 385 | + Map.Entry<K, V> ceilingEntry(K key); |
| 386 | + |
| 387 | + K ceilingKey(K key); |
| 388 | + |
| 389 | + Map.Entry<K, V> higherEntry(K key); |
| 390 | + |
| 391 | + K higherKey(K key); |
| 392 | + |
| 393 | + Map.Entry<K, V> firstEntry(); |
| 394 | + |
| 395 | + Map.Entry<K, V> lastEntry(); |
| 396 | + |
| 397 | + Map.Entry<K, V> pollFirstEntry(); |
| 398 | + |
| 399 | + Map.Entry<K, V> pollLastEntry(); |
| 400 | + |
| 401 | + NavigableMap<K, V> descendingMap(); |
| 402 | + |
| 403 | + NavigableSet<K> navigableKeySet(); |
| 404 | + |
| 405 | + NavigableSet<K> descendingKeySet(); |
| 406 | +} |
| 407 | + |
221 | 408 | class HashMap /* implements Map */{
|
222 | 409 | // unsorted, unordered
|
223 | 410 | // key's hashCode() is used
|
|
0 commit comments