Java का LinkedHashMap
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Insertion Order Preserved
HashMap के विपरीत, LinkedHashMap हमेशा अपनी entries को बिल्कुल उस order में iterate करता है जिसमें वे originally insert हुई थीं, और एक मौजूदा key की value update करना order में इसकी position नहीं बदलता।
उदाहरण: Insertion Order Preserved
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
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedHashMap vs HashMap
LinkedHashMap put, get, और remove के लिए HashMap जैसी ही average-case performance प्रदान करता है, linked list maintain करने के लिए थोड़ी ज़्यादा memory की कीमत पर, एक guaranteed, predictable iteration order के बदले।
उदाहरण: LinkedHashMap vs HashMap
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
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedHashMap vs TreeMap
LinkedHashMap TreeMap से इसमें अलग है कि यह कौन सा order maintain करता है: LinkedHashMap entries insert होने का order संरक्षित रखता है, जबकि TreeMap हमेशा entries को उनकी natural ordering या एक supplied Comparator के अनुसार key से sorted रखता है।
उदाहरण: LinkedHashMap vs TreeMap
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
}
}
Login to try C/C++/Java/PHP code in the editor
When to Use LinkedHashMap
LinkedHashMap सही choice है जब भी fast key-based lookups चाहिए लेकिन entries जोड़े जाने का order भी मायने रखता है, जैसे एक simple least-recently-used cache implement करना या एक visit history संरक्षित रखना।
उदाहरण: When to Use LinkedHashMap
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
}
}
Login to try C/C++/Java/PHP code in the editor
- Sorted order की उम्मीद करना, जब यह insertion order रखता है।
- इसे उपयोग करना जब order मायने नहीं रखता, जो कुछ memory खर्च करता है।
- यह मान लेना कि एक मौजूदा key फिर insert करना इसे आखिर में move कर देता है, जब original position default रूप से रखा जाता है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting