|
| 1 | +import java.util.ArrayDeque; |
| 2 | +import java.util.Arrays; |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Deque; |
| 5 | +import java.util.Iterator; |
| 6 | + |
| 7 | +/* |
| 8 | + * A deque (double-ended queue) functions as queues (first-in, first-out) or |
| 9 | + * as stacks (last-in, first-out). |
| 10 | + */ |
| 11 | + |
| 12 | +public class Deque_Learn { |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + |
| 16 | + /* |
| 17 | + * The 'Deque' interface extends 'Queue' interface and declares the behaviour of |
| 18 | + * a double-ended queue. 'Deque' being an interface needs a class which provides |
| 19 | + * implementation to its methods and also that we can create objects of that |
| 20 | + * class. 'ArrayDeque' class is used used while creating a deque object. |
| 21 | + * |
| 22 | + * The 'ArrayDeque' class extends 'AbstractCollection' class and implements the |
| 23 | + * 'Deque' interface. 'ArrayDeque' creates a dynamic array and has no capacity |
| 24 | + * restrictions. |
| 25 | + */ |
| 26 | + |
| 27 | + Deque<Integer> demoDeque = new ArrayDeque<>(); |
| 28 | + |
| 29 | + /* |
| 30 | + * Adding elements to deque |
| 31 | + * |
| 32 | + * boolean add(E obj) : Declared in the Collection interface. Adds object to the |
| 33 | + * collection. Returns true if object was added, otherwise returns false. |
| 34 | + * |
| 35 | + * boolean offer(E obj) : Declared in the Queue interface. Adds object to the |
| 36 | + * queue. Returns true if object was added, otherwise returns false. |
| 37 | + * |
| 38 | + * void addFirst(E obj) : Declared in the Deque interface. Adds obj to the head |
| 39 | + * of the deque. Throws an IllegalStateException if a capacity-restricted deque |
| 40 | + * is out of space. |
| 41 | + * |
| 42 | + * void addLast(E obj) : Declared in the Deque interface. Adds obj to the tail |
| 43 | + * of the deque. Throws an IllegalStateException if a capacity-restricted deque |
| 44 | + * is out of space. |
| 45 | + * |
| 46 | + * boolean offerFirst(E obj) : Declared in the Deque interface. Attempts to add |
| 47 | + * obj to the head of the deque. Returns true if obj was added and false |
| 48 | + * otherwise. |
| 49 | + * |
| 50 | + * boolean offerLast(E obj) : Declared in the Deque interface. Attempts to add |
| 51 | + * obj to the tail of the deque. Returns true if obj was added and false |
| 52 | + * otherwise. |
| 53 | + * |
| 54 | + * void push(E obj) : Declared in the Deque interface. Adds obj to the head of |
| 55 | + * the deque. Throws an IllegalStateException if a capacity-restricted deque is |
| 56 | + * out of space. |
| 57 | + */ |
| 58 | + |
| 59 | + demoDeque.add(10); |
| 60 | + // demoDeque = [10] |
| 61 | + |
| 62 | + demoDeque.offer(20); |
| 63 | + // demoDeque = [10, 20] |
| 64 | + |
| 65 | + demoDeque.addFirst(30); |
| 66 | + // demoDeque = [30, 10, 20] |
| 67 | + |
| 68 | + demoDeque.addLast(40); |
| 69 | + // demoDeque = [30, 10, 20, 40] |
| 70 | + |
| 71 | + demoDeque.offerFirst(50); |
| 72 | + // demoDeque = [50, 30, 10, 20, 40] |
| 73 | + |
| 74 | + demoDeque.offerLast(60); |
| 75 | + // demoDeque = [50, 30, 10, 20, 40, 60] |
| 76 | + |
| 77 | + demoDeque.push(70); |
| 78 | + // demoDeque = [70, 50, 30, 10, 20, 40, 60] |
| 79 | + |
| 80 | + System.out.println("demoDeque = " + demoDeque); // demoDeque = [70, 50, 30, 10, 20, 40, 60] |
| 81 | + |
| 82 | + /* |
| 83 | + * Removing elements from deque |
| 84 | + * |
| 85 | + * E remove() : Declared in the Queue interface. Removes the element at the head |
| 86 | + * of the queue returning the element in the process. It throws |
| 87 | + * NoSuchElementException if the queue is empty. |
| 88 | + * |
| 89 | + * boolean remove(Object obj) : Declared in the Collection interface. Removes |
| 90 | + * one instance of obj from the queue. Returns true if the element was removed. |
| 91 | + * Otherwise, returns false. |
| 92 | + * |
| 93 | + * E removeFirst() : Declared in the Deque interface. Removes the element at the |
| 94 | + * head of the deque returning the element in the process. It throws |
| 95 | + * NoSuchElementException if the deque is empty. |
| 96 | + * |
| 97 | + * E removeLast() : Declared in the Deque interface. Removes the element at the |
| 98 | + * tail of the deque returning the element in the process. It throws |
| 99 | + * NoSuchElementException if the deque is empty. |
| 100 | + * |
| 101 | + * boolean removeFirstOccurrence(Object obj) : Declared in the Deque interface. |
| 102 | + * Removes the first occurrence of obj from the deque. Returns true if |
| 103 | + * successful and false if the deque did not contain obj. |
| 104 | + * |
| 105 | + * boolean removeLastOccurrence(Object obj) : Declared in the Deque interface. |
| 106 | + * Removes the last occurrence of obj from the deque. Returns true if successful |
| 107 | + * and false if the deque did not contain obj. |
| 108 | + */ |
| 109 | + |
| 110 | + // demoDeque = [70, 50, 30, 10, 20, 40, 60] |
| 111 | + demoDeque.remove(); |
| 112 | + // demoDeque = [50, 30, 10, 20, 40, 60] |
| 113 | + |
| 114 | + demoDeque.remove(10); |
| 115 | + // demoDeque = [50, 30, 20, 40, 60] |
| 116 | + |
| 117 | + demoDeque.removeFirst(); |
| 118 | + // demoDeque = [30, 20, 40, 60] |
| 119 | + |
| 120 | + demoDeque.removeLast(); |
| 121 | + // demoDeque = [30, 20, 40] |
| 122 | + |
| 123 | + demoDeque.addLast(20); |
| 124 | + // demoDeque = [30, 20, 40, 20] |
| 125 | + |
| 126 | + demoDeque.removeFirstOccurrence(20); |
| 127 | + // demoDeque = [30, 40, 20] |
| 128 | + |
| 129 | + demoDeque.addFirst(20); |
| 130 | + // demoDeque = [20, 30, 40, 20] |
| 131 | + |
| 132 | + demoDeque.removeLastOccurrence(20); |
| 133 | + // demoDeque = [20, 30, 40] |
| 134 | + |
| 135 | + System.out.println("demoDeque = " + demoDeque); // demoDeque = [20, 30, 40] |
| 136 | + |
| 137 | + /* |
| 138 | + * Get the head / tail element of deque |
| 139 | + * |
| 140 | + * E peek() : Declared in the Queue interface. Returns the element at the head |
| 141 | + * of the queue. It returns null if the queue is empty. |
| 142 | + * |
| 143 | + * E peekFirst() : Declared in the Deque interface. Returns the element at the |
| 144 | + * head of the deque. It returns null if the deque is empty. The object is not |
| 145 | + * removed. |
| 146 | + * |
| 147 | + * E peekLast() : Declared in the Deque interface. Returns the element at the |
| 148 | + * tail of the deque. It returns null if the deque is empty. The object is not |
| 149 | + * removed. |
| 150 | + * |
| 151 | + * E getFirst() : Declared in the Deque interface. Returns the first element in |
| 152 | + * the deque. The object is not removed from the deque. It throws |
| 153 | + * NoSuchElementException if the deque is empty. |
| 154 | + * |
| 155 | + * E getLast() : Declared in the Deque interface. Returns the last element in |
| 156 | + * the deque. The object is not removed from the deque. It throws |
| 157 | + * NoSuchElementException if the deque is empty. |
| 158 | + */ |
| 159 | + |
| 160 | + // demoDeque = [20, 30, 40] |
| 161 | + int headElement = demoDeque.peek(); |
| 162 | + |
| 163 | + System.out.println("headElement = " + headElement); // headElement = 20 |
| 164 | + |
| 165 | + headElement = demoDeque.peekFirst(); |
| 166 | + |
| 167 | + System.out.println("headElement = " + headElement); // headElement = 20 |
| 168 | + |
| 169 | + int tailElement = demoDeque.peekLast(); |
| 170 | + |
| 171 | + System.out.println("tailElement = " + tailElement); // tailElement = 40 |
| 172 | + |
| 173 | + headElement = demoDeque.getFirst(); |
| 174 | + |
| 175 | + System.out.println("headElement = " + headElement); // headElement = 20 |
| 176 | + |
| 177 | + tailElement = demoDeque.getLast(); |
| 178 | + |
| 179 | + System.out.println("tailElement = " + tailElement); // tailElement = 40 |
| 180 | + |
| 181 | + /* |
| 182 | + * Get & remove the head / tail element of deque |
| 183 | + * |
| 184 | + * E poll() : Declared in the Queue interface. Returns the element at the head |
| 185 | + * of the queue, removing the element in the process. It returns null if the |
| 186 | + * queue is empty. |
| 187 | + * |
| 188 | + * E pop() : Declared in the Deque interface. Returns the element at the head of |
| 189 | + * the deque, removing it in the process. It throws NoSuchElementException if |
| 190 | + * the deque is empty. |
| 191 | + * |
| 192 | + * E pollFirst() : Declared in the Deque interface. Returns the element at the |
| 193 | + * head of the deque, removing the element in the process. It returns null if |
| 194 | + * the deque is empty. |
| 195 | + * |
| 196 | + * E pollLast() : Declared in the Deque interface. Returns the element at the |
| 197 | + * tail of the deque, removing the element in the process. It returns null if |
| 198 | + * the deque is empty. |
| 199 | + */ |
| 200 | + |
| 201 | + demoDeque.addFirst(10); |
| 202 | + demoDeque.addLast(50); |
| 203 | + |
| 204 | + // demoDeque = [10, 20, 30, 40, 50] |
| 205 | + int removedElement = demoDeque.poll(); |
| 206 | + |
| 207 | + System.out.println("removedElement = " + removedElement); // removedElement = 10 |
| 208 | + |
| 209 | + // demoDeque = [20, 30, 40, 50] |
| 210 | + removedElement = demoDeque.pop(); |
| 211 | + |
| 212 | + System.out.println("removedElement = " + removedElement); // removedElement = 20 |
| 213 | + |
| 214 | + // demoDeque = [30, 40, 50] |
| 215 | + removedElement = demoDeque.pollFirst(); |
| 216 | + |
| 217 | + System.out.println("removedElement = " + removedElement); // removedElement = 30 |
| 218 | + |
| 219 | + // demoDeque = [40, 50] |
| 220 | + removedElement = demoDeque.pollLast(); |
| 221 | + |
| 222 | + System.out.println("removedElement = " + removedElement); // removedElement = 50 |
| 223 | + |
| 224 | + System.out.println("demoDeque = " + demoDeque); // demoDeque = [40] |
| 225 | + |
| 226 | + /* |
| 227 | + * Get the count of elements present in the deque |
| 228 | + * |
| 229 | + * int size() : Declared in the Collection interface. Returns the number of |
| 230 | + * elements held in the invoking collection. |
| 231 | + */ |
| 232 | + |
| 233 | + // demoDeque = [40] |
| 234 | + int dequeSize = demoDeque.size(); |
| 235 | + |
| 236 | + System.out.println("Size = " + dequeSize); // Size = 1 |
| 237 | + |
| 238 | + /* |
| 239 | + * Check if deque is empty or not |
| 240 | + * |
| 241 | + * boolean isEmpty() : Declared in the Collection interface. Returns true if the |
| 242 | + * invoking collection is empty. Otherwise, returns false. |
| 243 | + */ |
| 244 | + |
| 245 | + if (demoDeque.isEmpty()) |
| 246 | + System.out.println("Deque is empty !"); |
| 247 | + else |
| 248 | + System.out.println("Deque is not empty !"); |
| 249 | + |
| 250 | + /* |
| 251 | + * Check if an object is present the deque |
| 252 | + * |
| 253 | + * boolean contains(Object obj) : Declared in the Collection interface. Returns |
| 254 | + * true if obj is an element of the invoking collection. Otherwise, returns |
| 255 | + * false. |
| 256 | + */ |
| 257 | + |
| 258 | + int value = 40; |
| 259 | + |
| 260 | + if (demoDeque.contains(value)) |
| 261 | + System.out.println("Deque contains " + value); |
| 262 | + else |
| 263 | + System.out.println("Deque does not contain " + value); |
| 264 | + |
| 265 | + /* |
| 266 | + * Clear the deque |
| 267 | + * |
| 268 | + * void clear() : Declared in the Collection interface. Removes all elements |
| 269 | + * from the invoking collection. |
| 270 | + */ |
| 271 | + |
| 272 | + demoDeque.clear(); |
| 273 | + // demoDeque = [] |
| 274 | + |
| 275 | + System.out.println("demoDeque = " + demoDeque); // demoDeque = [] |
| 276 | + |
| 277 | + /* |
| 278 | + * Construct deque from array |
| 279 | + */ |
| 280 | + |
| 281 | + String fruits[] = { "apple", "grape", "banana", "orange" }; |
| 282 | + |
| 283 | + Deque<String> fruitDeque = new ArrayDeque<>(); |
| 284 | + |
| 285 | + Collections.addAll(fruitDeque, fruits); |
| 286 | + |
| 287 | + System.out.println("fruitDeque = " + fruitDeque); // fruitDeque = [apple, grape, banana, orange] |
| 288 | + |
| 289 | + /* |
| 290 | + * Construct array from deque |
| 291 | + */ |
| 292 | + |
| 293 | + String fruitArr[] = fruitDeque.toArray(new String[fruitDeque.size()]); |
| 294 | + |
| 295 | + System.out.println("fruitArr = " + Arrays.toString(fruitArr)); // fruitArr = [apple, grape, banana, orange] |
| 296 | + |
| 297 | + /* |
| 298 | + * Iterating over the contents of a queue |
| 299 | + * |
| 300 | + * Iterator<E> iterator() : Declared in the Collection interface. Returns an |
| 301 | + * iterator for the invoking collection. |
| 302 | + * |
| 303 | + * Iterator<E> descendingIterator() : Declared in the Deque interface. Returns |
| 304 | + * an iterator that moves from the tail to the head (reverse iterator) of the |
| 305 | + * deque. |
| 306 | + */ |
| 307 | + |
| 308 | + Iterator itr = fruitDeque.iterator(); |
| 309 | + |
| 310 | + while (itr.hasNext()) { |
| 311 | + System.out.print(itr.next() + " "); |
| 312 | + } |
| 313 | + |
| 314 | + System.out.println(); |
| 315 | + |
| 316 | + Iterator reverseItr = fruitDeque.descendingIterator(); |
| 317 | + |
| 318 | + while (reverseItr.hasNext()) { |
| 319 | + System.out.print(reverseItr.next() + " "); |
| 320 | + } |
| 321 | + |
| 322 | + System.out.println(); |
| 323 | + |
| 324 | + // Iterate using for-each loop |
| 325 | + |
| 326 | + for (String fruit : fruitDeque) { |
| 327 | + System.out.print(fruit + " "); |
| 328 | + } |
| 329 | + |
| 330 | + } |
| 331 | + |
| 332 | +} |
0 commit comments