← Back to Advanced Java Course | Chapter 5: Collections Advanced | Lesson 2 of 4

Java का LinkedHashMap

एक LinkedHashMap एक ऐसा map है जो items जोड़े जाने का order याद रखता है, जैसे एक phone book जो entries को आपके लिखे order में रखती है। इसका उपयोग एक cache बनाने के लिए भी किया जा सकता है।
Syntax
java
LinkedHashMap<KeyType, ValueType> map = new LinkedHashMap<>();
map.put(key, value);  // iteration follows insertion order

What is a LinkedHashMap?

एक LinkedHashMap अपनी सभी entries से गुज़रती एक internal doubly-linked list बनाए रखता है। एक साधारण HashMap के विपरीत, जिसका iteration order अनिर्दिष्ट है और runs के बीच बदल भी सकता है, एक LinkedHashMap keys के मूल रूप से insert होने के order को संरक्षित रखता है, iteration को predictable बनाते हुए।

उदाहरण: What is a LinkedHashMap?

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
		map.put("c", 3); map.put("a", 1); map.put("b", 2);
		System.out.println(map); // preserves insertion order: c, a, b
	}
}

Access-Order Mode

आप किसी LinkedHashMap को default insertion-order mode के बजाय access-order mode उपयोग करने के लिए configure कर सकते हैं, जो किसी entry को हर बार पढ़े जाने पर अपने आप iteration order के आखिर में ले जाता है, सिर्फ लिखे जाने पर नहीं — LRU eviction policy का मूल building block।

उदाहरण: Access-Order Mode

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>(16, 0.75f, true); // access-order
		map.put("a", 1); map.put("b", 2); map.put("c", 3);
		map.get("a"); // moves "a" to the end
		System.out.println(map);
	}
}

Building an LRU Cache

आप access-order mode में LinkedHashMap को extend करके और इसके removeEldestEntry() method को override करके एक Least Recently Used (LRU) cache बना सकते हैं ताकि map आपके चुने maximum size से ऊपर जाते ही true return करे। फिर map हर insertion पर अपने आप सबसे पुरानी, सबसे-कम-हाल-में-उपयोग हुई entry को आपके लिए evict कर देता है।

उदाहरण: Building an LRU Cache

java
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<Integer, String> cache = new LinkedHashMap<>(16, 0.75f, true) {
			protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
				return size() > 2;
			}
		};
		cache.put(1, "a"); cache.put(2, "b"); cache.put(3, "c"); // evicts 1
		System.out.println(cache);
	}
}

Iteration and Performance

किसी LinkedHashMap को iterate करना आमतौर पर एक जैसे size के साधारण HashMap को iterate करने से थोड़ा तेज़ है, क्योंकि LinkedHashMap का iteration time सिर्फ असल में मौजूद entries की संख्या पर निर्भर करता है, जबकि HashMap का iteration time underlying table की पूरी capacity पर निर्भर करता है।

उदाहरण: Iteration and Performance

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
		for (int i = 0; i < 5; i++) map.put(i, "v" + i);
		for (var entry : map.entrySet()) { /* iteration cost depends only on size, not capacity */ }
		System.out.println("Entries: " + map.size());
	}
}

Key Operations and Null Values

एक standard HashMap की तरह, एक LinkedHashMap बिल्कुल एक null key और किसी भी संख्या में null values को support करता है, वह सब करते हुए भी चाहे जिस भी ordering mode — insertion या access — के साथ configure किया गया हो उसे ईमानदारी से बनाए रखते हुए।

उदाहरण: Key Operations and Null Values

java
// Import java.util.LinkedHashMap so it can be used by its short name
import java.util.LinkedHashMap;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new LinkedHashMap object and store it in map
		LinkedHashMap<String, String> map = new LinkedHashMap<>();
		map.put(null, "nullKeyValue");
		map.put("a", null);
		// Print a line to the console
		System.out.println(map);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि एक LinkedHashMap keys को sort करेगा, जबकि यह insertion order (या access order) रखता है और sort नहीं करता।
  2. तीसरा constructor argument true भूल जाना, जैसे new LinkedHashMap<>(16, 0.75f, true), जिससे access-order mode कभी enable नहीं होता।
  3. removeEldestEntry को override किए बिना एक LRU cache बनाना, जिससे पुरानी entries कभी नहीं हटतीं और map बढ़ता रहता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.