← Back to Core Java Course | Chapter 11: Collections | Lesson 10 of 17

Java का LinkedHashMap

LinkedHashMap एक hash table को एक internal linked list के साथ combine करता है, इसे HashMap की fast lookups के साथ एक predictable, insertion-ordered iteration देते हुए।
Syntax
java
LinkedHashMap<KeyType, ValueType> map = new LinkedHashMap<>();
map.put(key, value);  // keeps insertion order

Introduction to LinkedHashMap

LinkedHashMap Map का एक implementation है जो एक hash table को एक internal doubly-linked list के साथ combine करता है, इसे HashMap की fast lookup speed और एक predictable iteration order दोनों देते हुए।

उदाहरण: Introduction to LinkedHashMap

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); // hash table + linked list
		map.put("a", 1);
		System.out.println(map);
	}
}

Insertion Order Preserved

HashMap के विपरीत, LinkedHashMap हमेशा अपनी entries को बिल्कुल उस order में iterate करता है जिसमें वे originally insert हुई थीं, और एक मौजूदा key की value update करना order में इसकी position नहीं बदलता।

उदाहरण: Insertion Order Preserved

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
		map.put("banana", 1);
		map.put("apple", 2);
		map.put("banana", 99); // update doesn't move position
		System.out.println(map); // still banana, apple order
	}
}

LinkedHashMap vs HashMap

LinkedHashMap put, get, और remove के लिए HashMap जैसी ही average-case performance प्रदान करता है, linked list maintain करने के लिए थोड़ी ज़्यादा memory की कीमत पर, एक guaranteed, predictable iteration order के बदले।

उदाहरण: LinkedHashMap vs HashMap

java
import java.util.LinkedHashMap;
import java.util.HashMap;
public class Main {
	public static void main(String[] args) {
		HashMap<String, Integer> hash = new HashMap<>();
		LinkedHashMap<String, Integer> linked = new LinkedHashMap<>();
		hash.put("z", 1); hash.put("a", 2);
		linked.put("z", 1); linked.put("a", 2);
		System.out.println(linked); // predictable order, unlike hash
	}
}

LinkedHashMap vs TreeMap

LinkedHashMap TreeMap से इसमें अलग है कि यह कौन सा order maintain करता है: LinkedHashMap entries insert होने का order संरक्षित रखता है, जबकि TreeMap हमेशा entries को उनकी natural ordering या एक supplied Comparator के अनुसार key से sorted रखता है।

उदाहरण: LinkedHashMap vs TreeMap

java
import java.util.LinkedHashMap;
import java.util.TreeMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> linked = new LinkedHashMap<>();
		TreeMap<String, Integer> tree = new TreeMap<>();
		linked.put("banana", 1); linked.put("apple", 2);
		tree.put("banana", 1); tree.put("apple", 2);
		System.out.println(linked); // insertion order
		System.out.println(tree); // sorted by key
	}
}

When to Use LinkedHashMap

LinkedHashMap सही choice है जब भी fast key-based lookups चाहिए लेकिन entries जोड़े जाने का order भी मायने रखता है, जैसे एक simple least-recently-used cache implement करना या एक visit history संरक्षित रखना।

उदाहरण: When to Use LinkedHashMap

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> recentSearches = new LinkedHashMap<>();
		recentSearches.put("java", 1);
		recentSearches.put("python", 2);
		System.out.println(recentSearches); // fast lookup + preserved order
	}
}
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. Sorted order की उम्मीद करना, जब यह insertion order रखता है।
  2. इसे उपयोग करना जब order मायने नहीं रखता, जो कुछ memory खर्च करता है।
  3. यह मान लेना कि एक मौजूदा key फिर insert करना इसे आखिर में move कर देता है, जब original position default रूप से रखा जाता है।

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.