Java का Map Interface
In this page:
Map<KeyType, ValueType> map = new HashMap<>();
map.put(key, value);
map.get(key);
What is the Map Interface?
Map interface key-value pairs का एक collection represent करता है, जहां हर key unique है और exactly एक value को map करता है, इसे हर जगह जहां data को position के बजाय एक identifier से जल्दी lookup करना हो go-to structure बनाते हुए।
उदाहरण: What is the Map Interface?
import java.util.Map;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30); // key maps to exactly one value
System.out.println(ages.get("Alice"));
}
}
Login to try C/C++/Java/PHP code in the editor
Map Implementations
HashMap, LinkedHashMap, और TreeMap Map के तीन standard implementations हैं: HashMap कोई ordering guarantee के बिना सबसे तेज़ lookups प्रदान करता है, LinkedHashMap insertion order संरक्षित रखता है, और TreeMap entries को key से sorted रखता है।
उदाहरण: Map Implementations
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> hash = new HashMap<>();
Map<String, Integer> linked = new LinkedHashMap<>(); // preserves insertion order
Map<String, Integer> tree = new TreeMap<>(); // sorted by key
hash.put("a", 1);
linked.put("a", 1);
tree.put("a", 1);
System.out.println(hash + " " + linked + " " + tree);
}
}
Login to try C/C++/Java/PHP code in the editor
Key-Value Pairs
एक Map data को pairs के रूप में store करता है, जहां put एक key को एक value के साथ associate करता है और get बाद में उसी key का उपयोग करके वह value retrieve करता है -- एक मौजूदा key के तहत एक नई value डालना पुरानी value को overwrite करता है एक दूसरी entry बनाने के बजाय।
उदाहरण: Key-Value Pairs
import java.util.Map;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
ages.put("Alice", 31); // overwrites the old value
System.out.println(ages.get("Alice"));
}
}
Login to try C/C++/Java/PHP code in the editor
Common Map Methods
Map interface put, get, remove, containsKey, और isEmpty जैसे methods define करता है जो हर implementation में consistently behave करते हैं, इसलिए HashMap, TreeMap, और LinkedHashMap के बीच switch करना यह नहीं बदलता कि map कैसे उपयोग होता है।
उदाहरण: Common Map Methods
// Import java.util.Map so it can be used by its short name
import java.util.Map;
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new HashMap object and store it in ages
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
// Print a line to the console
System.out.println(ages.containsKey("Alice"));
// Print a line to the console
System.out.println(ages.isEmpty());
ages.remove("Alice");
System.out.println(ages.isEmpty());
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating a Map
एक Map तीन तरीकों से iterate किया जा सकता है: keySet सिर्फ keys return करता है, values सिर्फ values return करता है, और entrySet दोनों को एक साथ Map.Entry objects के रूप में return करता है, जो एक map की पूरी contents पर loop करने का सबसे आम तरीका है।
उदाहरण: Iterating a Map
// Import java.util.Map so it can be used by its short name
import java.util.Map;
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new HashMap object and store it in ages
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
ages.put("Bob", 25);
for (Map.Entry<String, Integer> entry : ages.entrySet()) {
// Print a line to the console
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Login to try C/C++/Java/PHP code in the editor
- एक missing key पर
get()call करना औरnullresult उपयोग करना, जोNullPointerExceptionthrow करता है। - यह उम्मीद करना कि duplicate keys रखी जाएंगी, जब
put()पुरानी value replace करता है। - एक mutable object को key के रूप में उपयोग करना और इसे insertion के बाद बदलना, इसलिए यह अब नहीं ढूंढा जा सकता।
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