Tag: Java Collections

Core Java

Java ArrayDeque

Introduction: ArrayDeque in Java is a class that implements a Deque interface. It’s an array-based implementation of a double-ended queue. As the name suggests, a double-ended queue is a queue that allows us to add or remove items from both front and rear ends. Before we dive in, let’s quickly look at a few noteworthy […]

Be the First to comment. Read More
Core Java

PriorityBlockingQueue In Java

Introduction: A PriorityBlockingQueue in Java implements the BlockingQueue interface and supports the features of a PriorityQueue. So, what’s a BlockingQueue? Following holds true for any implementation of a BlockingQueue: While attempting to retrieve an element, a thread waits if the queue is empty In case of a bounded BlockingQueue implementation, the thread waits till it […]

Be the First to comment. Read More
Core Java

LinkedHashMap In Java

Introduction: LinkedHashMap is a Map implementation which maintains the insertion order of its elements. It extends the HashMap class:

Just like a HashMap, it allows storing one null key and many null values. Instantiating LinkedHashMap: We can use one of the following constructors to create a LinkedHashMap:

The very last constructor also accepts a […]

Be the First to comment. Read More
Core Java

Java equals() and hashCode()

Introduction: Java Object class provides basic implementation of methods – hashCode() and equals(). These methods are extremely useful especially when working with the Collection framework. The hash table implementations rely on these methods for storing and retrieving data. In this tutorial, we’ll learn about the contract between hashCode() and equals(), their default implementations. We’ll also […]

One comment Read More
Core Java

WeakHashMap In Java

Introduction: A WeakHashMap in Java is a pretty popular data-structure among mid to senior-level Java developers. The WeakHashMap class is present in the java.util package. It’s a Map implementation which stores weak references to its keys. An entry in a WeakHashMap gets automatically removed when the associated key loses all of its active strong and soft […]

Be the First to comment. Read More
Core Java

HashSet In Java

Introduction: HashSet in Java implements Set interface i.e. it doesn’t allow duplicates. It is internally backed by a HashMap which works on the principle of hashing.  We can store a null value in a HashSet. Its default capacity is 16 with a load factor of 0.75, where:

A Java HashSet is non-synchronized. Also, there’s no guarantee […]

Be the First to comment. Read More
Core Java

Sorting a HashMap In Java

Introduction: In this tutorial, we’ll learn how to sort a Java HashMap. We can sort a HashMap either by keys or by values. We’ll discuss both of these strategies. Sorting a Java HashMap: To follow along with the rest of the article, let’s first construct a HashMap:

where Student is a POJO with id and […]

Be the First to comment. Read More
Core Java

Java Convert a List to Array And Vice-Versa

Introduction: In this article, we’ll quickly learn how to convert a Java List (say an ArrayList) to an array and vice-versa. If you wish to learn more about an ArrayList in general, feel free to read our article on Java ArrayLists. Meanwhile, let’s get going! Convert Java List to Array: Converting a Java List to an array is […]

Be the First to comment. Read More
Core Java

Check If Two Lists Are Equal In Java

Introduction: Lists in Java are ordered by nature. So, two lists are considered to be equal if they contain the exact same elements in the same order. In this tutorial, we’ll see how to compare two Lists for equality in Java. We’ll also cover ways in which we can just compare the elements in two lists […]

Be the First to comment. Read More
Core Java

CopyOnWriteArrayList In Java

Introduction: CopyOnWriteArrayList in Java is a thread-safe implementation of a List interface. It belongs to the java.util.concurrent package and is an enhanced version of ArrayList implementation. As the name suggests, CopyOnWriteArrayList creates a cloned internal copy of the underlying ArrayList for each add() or set() operations. Due to this additional overhead cost, we should ideally use a CopyOnWriteArrayList only when […]

Be the First to comment. Read More