Posts

How HashMap get() Method Works Internally in Java

Image
The get() method in HashMap is one of the most commonly used operations. Although it looks like a simple key lookup, internally it involves hashing, bucket indexing, and collision handling. In this article, we’ll break down the internal working of HashMap.get() step by step with examples. 1. Hashing the Key When you call map.get(key) , the HashMap computes a hash value using the key’s hashCode() . In Java 8+, this value is further processed with bitwise operations to spread the hash bits and reduce collisions. int hash = hash(key.hashCode()); 2. Finding the Bucket The hash value is then converted into an index in the internal array (called table ) using: int index = (n - 1) & hash; // n = table.length This determines the bucket where the entry might be stored. 3. Searching Inside the Bucket Each bucket can contain: null → no entry stored A single key-value pair A linked list of nodes (if multiple keys hash to the same bucket) A red-blac...

30 key systems design concepts

Image
Introduction: System design can feel overwhelming — especially when vast topics like scalability, performance, reliability, and distributed architecture are thrown at you all at once. But once you master the foundational building blocks, designing robust and scalable systems transforms from stressful to doable. In this post, we’ll explore 30 essential system design concepts with explanation 1. Client–Server Architecture What it is: The foundational model: thin clients (browsers, mobiles, IoT) make requests; servers process logic and return responses. Servers can be single or a pool of machines. Why it matters: It separates concerns — UIs live in clients and heavy computation/state in servers — enabling centralized control, security, and shared business logic. Trade-offs & details: Clients must handle availability issues and degraded connectivity. This model fits web apps, but for extreme scale you need caching, CDNs, and stateless servers (see Diag...

Serialization, Deserialization and Externalization in Java

Image
In Java interviews, one of the most common topics you’ll encounter is object persistence —how Java objects are saved, transferred, and reconstructed. This is where concepts like Serialization , Deserialization , and Externalization come into play. These aren’t just theoretical ideas; they are crucial in distributed systems, caching, session management, and inter-process communication . Let’s explore these concepts step by step, highlight their differences, and discuss real-world use cases. 1. What is Serialization in Java? Serialization is the process of converting a Java object into a byte stream so that it can be stored in a file, transmitted over a network, or persisted in a database. How it works: The class must implement the Serializable interface (marker interface). Use ObjectOutputStream.writeObject() to serialize an object. Non-transient, non-static fields are serialized; transient fields are skipped. ...

Understanding the Proxy Design Pattern in Java

Image
The Proxy Design Pattern is a structural design pattern that provides a placeholder or surrogate for another object to control access to it. A proxy can be used to add additional functionality such as lazy initialization, access control, logging, or caching before or after a request reaches the original object. This post explains the Proxy Pattern with simple real-world examples like payment methods and ATM interactions based on the detailed tutorial. What is the Proxy Design Pattern? The Proxy Pattern involves a proxy object that implements the same interface as the original object and holds a reference to it. The proxy controls access, managing when or how the real object is accessed and manipulated. Real-World Example: Payment System When making payments, one might pay using different methods like cash, credit card, or UPI. These payment modes act as proxies to the actual cash system, adding convenience and flexibility while controlling access to real cash transactions. ...

Understanding the Facade Design Pattern in Java

Image
The Facade Design Pattern is a structural design pattern that provides a simplified interface to a complex system such as a library, framework, or a set of classes. It hides the complexities of the system and provides the client with an easier way to access functionalities. What is the Facade Design Pattern? The Facade Pattern offers a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use by encapsulating the interactions with multiple classes behind a single facade class. Real-World Analogy: Food Delivery App Consider a food delivery app like Zomato. The app serves as a facade that simplifies interactions between customers, restaurants, delivery teams, and delivery partners. Instead of customers interacting separately with all these entities, the app provides a single interface to place an order, which internally manages the complex processes like order preparation, assignment of delivery persons, a...

Understanding the Decorator Design Pattern in Java

Image
The Decorator Design Pattern is a structural design pattern that allows behavior to be added to individual objects dynamically without affecting other objects from the same class. It works by wrapping the original object inside special wrapper objects that add the new behavior. This approach is especially useful when there are many possible combinations of behaviors, and subclassing for each variation is not feasible. What is the Decorator Design Pattern? Decorator pattern lets you attach additional responsibilities to an object at runtime. Instead of creating many subclasses for every possible combination, decorators allow behaviors to be composed automatically in a flexible and reusable way. Real-World Analogy: Pizza Toppings Think about ordering a pizza. You start with a base pizza and then add various toppings like extra cheese, jalapenos, mushrooms, etc. Instead of creating a subclass for every topping combination, each topping acts as a decorator that wraps the pizza, ...

Understanding the Bridge Design Pattern in Java

Image
The Bridge Design Pattern is a structural design pattern that decouples an abstraction from its implementation, allowing them to vary independently. This pattern is especially useful when dealing with complex class hierarchies that would otherwise lead to an explosion of subclasses. This post explains the Bridge Pattern using a practical example of video streaming platforms and video quality options, demonstrating how to avoid excessive subclassing through composition. What is the Bridge Design Pattern? Instead of using inheritance to combine abstraction and implementation, the Bridge Pattern uses composition to separate these concerns into different class hierarchies. This approach prevents subclass explosion and provides more flexibility in combining behaviors at runtime. Problem Illustration Consider video streaming platforms like YouTube and Netflix offering various video qualities such as HD, 4K, and 8K. A naive design would create subclasses for every combination (e.g....