Posts

Showing posts with the label Java Internals

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...

Internal Working of Map & HashMap in Java (Interview Ready)

Image
🔹 Introduction In the Java Collections Framework, both Map and HashMap are essential for working with key-value pairs. This blog dives into their internal mechanics, including how objects are stored and retrieved, how collisions are managed, and what improvements Java 8 brought to the table. 1. What is a Map? A Map<K, V> is a data structure that stores unique keys associated with corresponding values. Common implementations include: HashMap – unordered, allows one null key and multiple null values TreeMap – sorted by keys LinkedHashMap – maintains insertion order 2. How HashMap Works Internally ➡️ Nodes & Buckets Internally, a HashMap uses an array of Node objects , where each node represents a key-value pair plus a pointer for chaining in case of collisions. class Node<K,V> { int hash; K key; V value; Node<K,V> next; } ➡️ Hashing & Index Calculation ...

Understanding Multiple Threads in Java

Image
Introduction In Java programming, achieving parallel execution of tasks is made possible using multithreading. This blog explores how to create and run multiple threads concurrently, why using start() is critical, how scheduling works, and additional tips like using sleep() for clearer outputs. Demonstrating Multiple Threads Let's start with a simple example. We define two threads—one to say “Hi” and another to say “Hello”. class HiThread extends Thread {   public void run() {     for(int i = 0; i < 5; i++) {       System.out.println("Hi");       try { Thread.sleep(500); } catch(Exception e) {}     }   } } class HelloThread extends Thread {   public void run() {     for(int i = 0; i < 5; i++) {       System.out.println("Hello");       try { Thread.sleep...

How Java Manages Memory Behind the Scenes

Image
Understanding how Java allocates and deallocates memory is essential for writing efficient, fast, and reliable applications. While the JVM automates most of the heavy lifting, knowing what happens under the hood helps you avoid memory leaks, improve performance, and truly master Java. 🔍 1. Where Java Data Lives Java stores data in different memory regions depending on whether it is a primitive value or a reference to an object . 🟦 a) Primitive Data Types Examples: int , double , boolean , char , byte , short , long , float Where they’re stored: Java Stack (local primitives) — When primitive variables are declared inside a method, they live on the stack. Heap (inside objects) — If primitives are part of an object, they live inside that object on the heap. public void myMethod() { int count = 10; // Stored on stack boolean isActive = true; } class MyClass { int value; // Stored on the heap as part of the obje...