Queue, Deque, Stacks, SortedMap, NavigableMap, Java 8 enhancements related to Map


Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies.


Queues

FIFO - First in first out

add() - Colletions have a method add to add an element to the queue. General functionality of add method returns true if the element is added and false if the element has not been added.
In case of queue, it returns true when the element has been added and throws exception when the queue is full
Offer() - This method returns true if the element has been added to the queue and returns false when the queue is full.

In case of adding elements to the queue, offer method should be used.

E remove() - Removes the element from the queue. remove throws exception when the queue is empty.
E poll() - returns null when the queue is empty

E element() - Just read elements without actually removing. Throws exception when the queue is empty
E peek() - Just read elements without actually removing. Returns null when the queue is empty

Priority Queue - Defines priority of the elements in the queue

Stacks

Last in First out data structure
It is deprecated because it is thread safe and is not good to use.

Deques - Correct way to use stacks
It has two ends. Add and remove from both ends but in case of queue we can add at one end and remove from another.

Deque extends Queue. So methods of the queue are inherited to the Deque.
Following are the methods specific to Deque

boolean offerFirst(E e)
boolean offerLast(E e)

addFirst
addLast

removeFirst
removeLast

pollFirst
pollLast

getFirst
getLast

peekFirst
peekLast

push(E e)
E pop()

- ArrayDeque
- LinkedList

Try to avoid using LinkedList and deprecated Stack implementation.

SortedMap and NavigableMap
- Defines the ordering of elements

SortedMap is introduced in java 5 and NavigableMap in Java 6 because SortedMap does not provide complete support and NavigableMap provide additional features over SortedMap

K firstKey
K lastKey

SortedMap<K,V> tailMap(E fromKey)
SortedMap<K,V> headMap(E fromKey)
SortedMap<K,V> subMap(Key k)

NavigableMap - Add lots of features over SortedMap. See more at Oracle docs

Java 8 enhancements of Map - See more at oracle docs about the below methods

replace(key, value) - replace the value of the key with new value
replaceAll(BiFunction<K,V,V>)
remove(key,value)

getOrDefault - returns default value reducing null pointer exception
putIfAbsent
compute - Takes a key and function and let you compute new value for key
computeIfPresent
computeIfAbsent
merge - Take a key and value and also older value and merge new and old value
forEach - Lambda expression used to print the key and values pairs.








Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights