Category Archives: Java

Java DelayQueue — A Specialized BlockingQueue for Delayed Elements

In concurrent programming, often you need to schedule work or tasks to happen after a delay: e.g. timeouts, scheduled retries, delayed tasks, rate limiting, delayed processing, etc. Java’s java.util.concurrent package provides a handy queue for such use cases — DelayQueue — which accepts elements that become available only after a specified delay.

In this post, we will explore:

  1. What is a DelayQueue
  2. The Delayed interface
  3. How DelayQueue works internally
  4. Key methods and behaviors
  5. A simple producer/consumer example
  6. Real-world use cases & caveats
  7. Summary

1. What is DelayQueue

DelayQueue<E extends Delayed> is an unbounded blocking queue whose elements must implement the Delayed interface. An element can be retrieved (via take() or poll(...)) only when its delay has expired. Until then, it stays “dormant” inside the queue.

Some important characteristics:

  • The head of the queue is the delayed element whose expiration (delay) is earliest (i.e. whose delay will expire first).
  • If no element has expired yet, poll() returns null, and take() blocks until an element becomes available.
  • The queue is unbounded — operations like put() or offer() never block (unless out of memory).
  • The queue uses internal locking (ReentrantLock) for thread safety.
  • Iterators over the queue are weakly consistent and do not guarantee ordered traversal.

In short: DelayQueue is like a priority queue (ordered by expiration time) combined with blocking/waiting semantics.

Continue reading Java DelayQueue — A Specialized BlockingQueue for Delayed Elements

Java BitSet Explained (with Practical Examples)

When developing applications, there are situations where we need to represent and manipulate large collections of binary values – typically true or false. Storing these values in a conventional data structure like a boolean[] or a HashSet<Integer> can be inefficient in terms of memory and speed.

Java provides the BitSet class in the java.util package to address this problem. BitSet represents a sequence of bits that can grow dynamically and provides built-in methods for bit-level manipulation.

In this article, we will cover:

  1. Introduction to BitSet
  2. Internal representation and advantages
  3. Common operations
  4. Basic usage example
  5. Real-world example: Attendance tracking
  6. Bitwise operations (AND, OR, XOR, ANDNOT)
  7. Performance comparison with boolean[] and HashSet
  8. Conclusion
Continue reading Java BitSet Explained (with Practical Examples)

Java Interview Preparation Guide

I’ve sat on both sides of the Java interview table enough times to notice a pattern: candidates who can recite definitions often freeze the moment you ask “why” or “what happens if.” This guide is organized the way interviews actually flow — core language fundamentals, then collections, then concurrency, then the Streams/Optional questions that trip up even experienced developers, and finally the Spring basics that show up in almost every Java backend role today. Each question includes a real, runnable example and a note on the specific way candidates get it wrong, because the wrong answer is usually more instructive than the right one.

Continue reading Java Interview Preparation Guide

Overview of New Features in Java 19

Java 19 has just been released — September 20, 2022 — as a short-term support release on the six-month cadence the Java platform moved to starting with Java 9. It is not an LTS release, but that does not make it uninteresting: Java 19 ships 7 JEPs, several of them highly anticipated by the Java community. Virtual Threads, in particular, represent one of the most significant changes to the Java concurrency model in years.

The headline features span concurrency, pattern matching, native interop, and platform reach. Three features arrive as preview (complete but seeking feedback), one as an incubator module (early-stage API), and the rest are fully stable additions. Because several require flags to enable, we will also cover exactly how to turn them on — both on the command line and in Maven.

This article walks through each major Java 19 feature with code examples and explains the preview and incubator status so you know what you can try today and what to expect in future releases.

Continue reading Overview of New Features in Java 19

Apache Commons Collection – MultiValuedMap

The first time I needed a one-to-many map in Java, I reached for Map<String, List<String>> and wrote the same computeIfAbsent boilerplate I’d written a dozen times before. Apache Commons Collections’ MultiValuedMap exists specifically to kill that boilerplate — it’s a Map variant that lets a single key hold multiple values natively, without you manually managing the inner list. This post covers what it actually buys you over rolling your own, how it stacks up against Guava’s Multimap, and a realistic use case: grouping form-validation errors by field name.

Here’s the minimal example to see the API shape:

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;

public class MultiValuedMapDemo {
    public static void main(String[] args) {
        MultiValuedMap<String, String> multiValuedMap = new ArrayListValuedHashMap<>();
        multiValuedMap.put("Key1", "Value1");
        multiValuedMap.put("Key1", "Value2");
        multiValuedMap.put("Key2", "Value3");
        System.out.println(multiValuedMap.get("Key1"));
    }
}
Continue reading Apache Commons Collection – MultiValuedMap