Tag Archives: Java

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.

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

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)

Apache Commons Collection – MultiValuedMap

Apache Commons Collection is a library of useful data structures, collections, and algorithms in Java. MultiValuedMap is one of the data structures present in the Apache Commons Collection. MultiValuedMap is a Map that allows multiple values for a single key. It is a useful implementation of a one-to-many relationship. You can add multiple values to the same key, and the key-value pairs are stored in the order in which they are added.

Here’s an example Java code of using MultiValuedMap:

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

Spring Cloud: Getting started with Hystrix Dashboard

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.

Continue reading Spring Cloud: Getting started with Hystrix Dashboard

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

In the last tutorial, we have seen Spring Cloud: Exploring Spring Cloud Config Server (Native Method). In this tutorial, we will look into the GIT base method.

Spring cloud config allows you to have applications/micro-services configuration at a centralized place. Since we are working on spring micro-services. We may have hundreds of micro-services running together. Now we want to manage configuration for hundreds of those micro-services. It would be a big pain if we do it manually. Instead, we will use the config server provided by Spring Cloud to manage that configuration from a central place.

We are going to achieve the same goal here which we discussed in the earlier tutorial. But here we will move our configuration to a GIT repository.

TL;DR You can download whole project by clicking following link.

Continue reading Spring Cloud: Exploring Spring Cloud Config Server (GIT Mode)

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

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.

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