Java का LinkedHashMap
In this page:
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?
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
}
}
Login to try C/C++/Java/PHP code in the editor
Access-Order Mode
आप किसी LinkedHashMap को default insertion-order mode के बजाय access-order mode उपयोग करने के लिए configure कर सकते हैं, जो किसी entry को हर बार पढ़े जाने पर अपने आप iteration order के आखिर में ले जाता है, सिर्फ लिखे जाने पर नहीं — LRU eviction policy का मूल building block।
उदाहरण: Access-Order Mode
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Iteration and Performance
किसी LinkedHashMap को iterate करना आमतौर पर एक जैसे size के साधारण HashMap को iterate करने से थोड़ा तेज़ है, क्योंकि LinkedHashMap का iteration time सिर्फ असल में मौजूद entries की संख्या पर निर्भर करता है, जबकि HashMap का iteration time underlying table की पूरी capacity पर निर्भर करता है।
उदाहरण: Iteration and Performance
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());
}
}
Login to try C/C++/Java/PHP code in the editor
Key Operations and Null Values
एक standard HashMap की तरह, एक LinkedHashMap बिल्कुल एक null key और किसी भी संख्या में null values को support करता है, वह सब करते हुए भी चाहे जिस भी ordering mode — insertion या access — के साथ configure किया गया हो उसे ईमानदारी से बनाए रखते हुए।
उदाहरण: Key Operations and Null Values
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक
LinkedHashMapkeys को sort करेगा, जबकि यह insertion order (या access order) रखता है और sort नहीं करता। - तीसरा constructor argument
trueभूल जाना, जैसेnew LinkedHashMap<>(16, 0.75f, true), जिससे access-order mode कभी enable नहीं होता। removeEldestEntryको override किए बिना एक LRU cache बनाना, जिससे पुरानी entries कभी नहीं हटतीं और map बढ़ता रहता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: