Saturday, May 22, 2021

Collections: Queue(s)

  • Queue
    • It is an interface and extends Collection interface
    • Most used implementations are Blocking Queue, Priority Queue & Linked List 
  • Blocking Queue
    • Are thread safe
    • Blocks the thread which tries to dequeue elements from an empty queue OR a thread which tries to insert elements in queue which is already full
    • Threads are blocked until an element is either inserted or removed respectively 
    • Do not accept NULL values
    • Used to implement producer/consumer based application
    • If operation is not possible immediately then (refer below table)

 

Throws Exception

Returns Special Value usually (true/false)

Blocks method call

Method call blocks & waits till Time Out

Insert

add(o)

offer(o)

put(o)

offer(o, timeout, timeunit)

Remove

remove(o)

poll()

take()

poll(timeout, timeunit)

Examine

element()

peek()

 

 

 

  • Priority Queue
    • Elements are inserted according to some priority order (not FIFO)
    • Either default natural sorting order or customized sorting order can be implemented
    • Insertion order not preserved
    • Duplicates are not allowed
    • Heterogeneous objects allowed ONLY for customized order, ONLY homogeneous for natural sorting order 
    • NULL elements NOT allowed
    • Constructors
    • Default: With initial capacity 11 and with natural sorting order
    • int initialCapacity = Creates with predefined size = initialCapacity
    • int initialCapacity, Comparator c: Creates with predefined size = initialCapacity and comparator for customized sorting
    • SortedSet s: convert SortedSet to PriorityQueue
    • Collection c: convert Collection to PriorityQueue
    • Some Operating Systems won’t provide proper support for thread priorities and PriorityQueue
  • Linked List
    • 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


Offer

Peak

Poll

Size

Data Structure

PriorityQueue

O(log n )

O(1)

O(log n)

O(1)

Priority Heap

LinkedList

O(1)

O(1)

O(1)

O(1)

Array

ArrayDequeue

O(1)

O(1)

O(1)

O(1)

Linked List

ConcurrentLinkedQueue

O(1)

O(1)

O(1)

O(n)

Linked List

ArrayBlockingQueue

O(1)

O(1)

O(1)

O(1)

Array

PriorirityBlockingQueue

O(log n)

O(1)

O(log n)

O(1)

Priority Heap

SynchronousQueue

O(1)

O(1)

O(1)

O(1)

None!

DelayQueue

O(log n)

O(1)

O(log n)

O(1)

Priority Heap

LinkedBlockingQueue

O(1)

O(1)

O(1)

O(1)

Linked List

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...