Saturday, July 19, 2025

What is PriorityQueue or priority queue in Java with Example - Tutorial

PriorityQueue is an unbounded Queue implementation in Java, which is based on a priority heap. PriorityQueue allows you to keep elements in a particular order, according to their natural order or custom order defined by the Comparator interface in Java. Head of priority queue data structure will always contain the least element with respect to specified ordering. For example, in this post, we will create a PriorityQueue of Items, which are ordered based upon their price, this will allow us to process Items, starting from the lowest price. A priority queue is also very useful in implementing the Dijkstra algorithm in Java. You can use to PriorityQueue to keep unsettled nodes for processing.

What is EnumMap in Java – Example Tutorial

What is EnumMap in Java
EnumMap in Java is added on JDK  5 release along with other important features like  Autoboxing, varargs, and Generics. EnumMap is a specialized Map implementation designed and optimized for using Java Enum as key. Since enum can represent a type (like class or interface)  in Java and it can also override equals() and hashCode(), It can be used inside HashMap or any other collection but using  EnumMap brings implementation-specific benefits which are done for enum keys, In short EnumMap is optimized Map implementation exclusively for enum keys. As per Javadoc Enum is implemented using Arrays and common operations result in constant time.

How to use CopyOnWriteArraySet in Java with Example

CopyOnWriteArraySet is little brother of CopyOnWriteArrayList class. These are special purpose collection classes which was added on JDK 1.5, along with their most popular cousin ConcurrentHashMap. They are part of concurrent collection framework and reside in java.util.concurrent package. CopyOnWriteArraySet is best suited as a read-only collection whose size is small enough to copy if some mutative operation happens, for example, you can use CopyOnWriteArraySet to store objects at the start-up of the application and let multiple application thread access them during application life time. If an new condition or object comes up during that time, it can also be added into this Set, with incurring cost of creating a new array.