Category: Java interview questions
-
How does TreeMap maintain its order?
1. Overview A TreeMap in Java is a Map implementation that keeps its keys sorted — either by natural ordering or by a custom comparator provided at creation. Example: Unlike HashMap, which uses hashing, TreeMap uses a balanced binary search tree to store its entries — specifically a Red-Black Tree. 2. Internal Data Structure Internally,…
-
What happens internally when we call put() in a hashmap?
1. Overview When you call: Java performs several internal operations — hashing, indexing, bucket management, collision handling, resizing, and finally storing the key-value pair. 2. Step-by-Step Internal Process of put() Let’s break it down: Step 1: Calculate the hash of the key If the key is null, hash = 0. Step 2: Determine the bucket…
-
Why HashMap Allows One Null Key While Hashtable Doesn’t?
1. Background Hashtable HashMap 2. Internal Working Difference Let’s look at how key lookup works in both: In HashMap In Hashtable 3. Design Rationale Aspect HashMap Hashtable Introduced in Java 1.2 (Collections Framework) Java 1.0 (Legacy class) Synchronization Non-synchronized Synchronized Null keys Allowed (one null key) Not allowed Null values Allowed Not allowed Performance Faster…
-
Implementing LRU Cache in Java
Caching is one of the most essential techniques for improving performance in systems that require fast access to frequently used data. An LRU (Least Recently Used) Cache is a type of cache algorithm that evicts the least recently used items first when the cache reaches its maximum capacity. In this tutorial, we’ll cover: 1. What…
-
Parallel Streams vs. ForkJoinPool
Parallel Streams in Java are a high-level API for processing collections in parallel, while the ForkJoinPool is the low-level framework that powers this parallelism under the hood. The key difference is that a parallel stream is a convenient abstraction that leverages the ForkJoinPool for you, whereas the ForkJoinPool is a direct, manual-control framework you use…
-
Programmatic vs Declarative Transaction Management in Spring – When to Use Which
Spring Framework provides two primary approaches for managing transactions: Both approaches use Spring’s PlatformTransactionManager under the hood but differ in how and where the transaction logic is applied. 1. What Is Transaction Management in Spring? Transaction management ensures data consistency in the event of success or failure in a group of operations (e.g., a database…
-
How Spring Boot Auto-Configuration Internally Uses @ConditionalOnClass and @ConditionalOnMissingBean
Spring Boot’s auto-configuration mechanism simplifies application setup by automatically configuring beans based on the presence of classes, beans, or properties. Two key annotations that power this mechanism are: Understanding how these work is critical to mastering Spring Boot internals and customizing behavior effectively. 1. What is Auto-Configuration in Spring Boot? Spring Boot provides opinionated defaults…
-
How to Tune the JVM for a Low-Latency Trading-like System
In low-latency systems (like high-frequency trading platforms), response time and predictability are critical. The Java Virtual Machine (JVM), being managed and garbage-collected, requires careful tuning to minimize latency, avoid pauses, and meet real-time constraints. This guide focuses on tuning the JVM for: 1. Understand the Requirements of Low-Latency Systems Before tuning, understand what “low-latency” implies:…
-
How to Implement a Custom ClassLoader for Hot-Reloading in Java
Hot-reloading is the ability to reload classes during runtime without restarting the JVM. It’s commonly used in development environments (like Spring Boot devtools, JRebel) or plugin systems where new or modified code needs to be picked up dynamically. At the core of hot-reloading in Java is a custom ClassLoader. 1. Why Use a Custom ClassLoader…
-
Blocking Queues vs Non-Blocking (Lock-Free) Data Structures in Java
Java provides multiple concurrency utilities to handle thread-safe data sharing, particularly useful in producer-consumer patterns. Two such key categories are: 1. What is a Blocking Queue? A blocking queue is a thread-safe data structure that supports operations which wait (block) for the queue to become non-empty when retrieving an element, or wait for space to…
