Skip to main content

Java

Java Hashtable: A Dive into Synchronized HashMaps

In the world of Java, when you need to store key-value pairs, HashMap is often your first thought. But what if you need thread-safety—what if multiple threads need to access and modify your map concurrently without corrupting its data? Enter Hashtable. While Hashtable might seem like an older, perhaps less frequently used, sibling to HashMap, it offers a distinct advantage: built-in synchronization. Let's explore Hashtable in detail, understanding its characteristics, how to use it, and when it's the right choice. What is Hashtable? java.util.Hashtable is a concrete implementation of the Map interface in Java. It stores data in key-value pairs and uses a hashing mechanism to efficiently store and retrieve objects. Key characteristics include: Synchronization: All public methods of Hashtable are synchronized. This means that only one thread can access a Hashtable instance at a time, ensuring data consistency in a multi-threaded environment. Nulls Not Allowed: Unlike HashMap, Hashtable does not allow null keys or null values. Attempting to insert a null key or value will result in a NullPointerException. Legacy Class: Hashtable is part of Java's legacy collections framework, dating back to Java 1.0. While functional, newer (and often more performant) synchronized map implementations like ConcurrentHashMap are generally preferred for new development. Initial Capacity and Load Factor: Hashtable uses an initial capacity (default 11) and a load factor (default 0.75). When the number of entries exceeds (capacity * load factor), the Hashtable automatically rehashes and increases its capacity.

Tail vs Non-Tail Recursion in Java: Definitions, Examples, and When It Matters

Quick summary Tail recursion is when a function’s recursive call is the last operation before returning, enabling tail-call elimination in languages/runtimes that support it; non-tail recursion performs additional work after the recursive call returns. In Java, tail-call optimization is not guaranteed by the JVM, so tail recursion does not reduce stack usage unless transformed to iterative code; still, converting non-tail recursion to tail style can make iterative conversion straightforward and avoid stack overflow in production. What is recursion? Recursion is a divide-and-conquer technique where a function calls itself with smaller inputs until a base case is reached, with the JVM allocating a stack frame for each invocation; deep recursion risks StackOverflowError if not controlled. Each call must eventually reduce the problem and hit a base case to terminate safely and predictably.

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: What is a DelayQueue The Delayed interface How DelayQueue works internally Key methods and behaviors A simple producer/consumer example Real-world use cases & caveats 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.

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: Introduction to BitSet Internal representation and advantages Common operations Basic usage example Real-world example: Attendance tracking Bitwise operations (AND, OR, XOR, ANDNOT) Performance comparison with boolean[] and HashSet Conclusion

Apache Commons Collection – MultiValuedMap

Apache Commons Collections MultiValuedMap compared against Guava Multimap and a vanilla nested Map of Lists, with a realistic form-validation-errors use case, annotated code, sample output, and FAQs.

Spring Cloud: Getting started with Hystrix Dashboard

⚠️ This tutorial is outdated. Hystrix Dashboard was removed from Spring Cloud and does not work with Spring Boot 3.x. The modern approach is Resilience4j metrics with Prometheus & Grafana — see the Spring Cloud Netflix migration guide. This post remains online for teams maintaining legacy systems. This is a quick tutorial on Hystrix dashboard. Hystrix dashboard allows you to view the overall status of your Spring cloud application at a single glance. It provides access to vital metrics of your application and gives you a graphical representation of those for better understanding. This post is the continuation of Spring Cloud: Adding Hystrix Circuit Breaker and Spring Cloud: Playing with Hystrix Circuit Breaker. Please go through those post, if you haven't. Those posts explain about Hystrix circuit breaker. TL;DR You can download whole project by clicking following link. Spring Cloud (V2.3.1) Hystrix DashboardDownload

Spring Cloud Config Server on Spring Boot 3.x: Git Mode, Native Mode, and Runtime Refresh

Spring Cloud Config is, alongside Eureka, one of the two survivors of the original Spring Cloud stack — still maintained, still the standard answer for centralized configuration outside Kubernetes. This post is a complete rewrite of my 2020 Config Server tutorials for Spring Boot 3.x: a Config Server backed by Git (with native/filesystem mode for local development), clients using the modern spring.config.import mechanism instead of the long-gone bootstrap.properties, and runtime refresh that actually works.

Spring Cloud: Exploring Spring Cloud Config Server (Native Mode)

⚠️ This tutorial covers an old Spring Cloud version. Spring Cloud Config is still maintained, but this setup targets Spring Boot 2.x. For Spring Boot 3.x configuration (Git and native mode), see the updated Spring Cloud Config Server guide and the Spring Cloud Netflix migration guide. This is a quick tutorial on Spring Cloud Config Server. In brief, Spring cloud config allows you to have applications/micro-services configuration at a centralized place. Since we are working on spring micro-services, in production we may have hundreds of micro-services running together. Now if we want to manage configuration for hundreds of those micro-services then it would be a big pain if we do it manually. Instead, we will use Spring cloud config server to manage that configuration from a central place. TL;DR You can download whole project by clicking following link. Spring Cloud (V2.3.1) Config Server in Native ModeDownload Spring Cloud (V2.3.1) Sample Configuration for Config Server in Native ModeDownload

Spring Cloud: Adding Filters in Zuul Gateway

⚠️ This tutorial is outdated. Zuul 1 (including its filter model) was removed from Spring Cloud and does not work with Spring Boot 3.x. Filters are now written as Gateway filters — see the Zuul to Spring Cloud Gateway migration guide and the Spring Cloud Netflix migration guide. This post remains online for teams maintaining legacy systems. This tutorial is the continuation of Spring Cloud: Exploring Zuul Gateway tutorial. In this tutorial, we will be exploring the functionality of filters provided by Zuul. As discussed earlier Zuul provides various filters which we can use for request validation or processing. Let's say if you have an incoming request and if you want to check whether the user is authenticated or not, you can use Zuul pre-filter for this.  If your request is processed and if you want to encrypt the response you can use Zuul post filter. The major upside of Zuul filter is, you can manage all of your filters at a centralized location. Zuul has provision to create following four types of filter. pre filters run before the request is routed.route filters can handle the actual routing of the request.post filters run after the request has been routed.error filters run if an error occurs while handling the request.