Java का HashMap
In this page:
HashMap<KeyType, ValueType> map = new HashMap<>();
map.put(key, value);
map.get(key);
map.containsKey(key);
Introduction to HashMap
HashMap key-value pairs store करता है और entries को internal buckets में रखने के लिए हर key की hashCode() उपयोग करता है, चाहे इसमें कितनी भी entries हों average O(1) lookup, insertion, और removal देते हुए।
उदाहरण: Introduction to HashMap
// 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 map
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
// Print a line to the console
System.out.println(map.get("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Removing and Clearing Elements
remove(key) key से एक single mapping delete करता है, जबकि clear() पूरा map एक बार में खाली करता है — दोनों operations map की size को तुरंत invalidate करते हैं बिना इसे rebuild करने की ज़रूरत के।
उदाहरण: Removing and Clearing Elements
// 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 map
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
map.remove("apple");
// Print a line to the console
System.out.println(map);
map.clear();
// Print a line to the console
System.out.println(map.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over HashMap
एक HashMap को iterate करने का आमतौर पर मतलब है key और value दोनों एक साथ पाने के लिए entrySet() पर loop करना, या जब आपको सिर्फ एक side चाहिए तो keySet()/values() — HashMap entries किस order में वापस आती हैं इसकी कोई guarantee नहीं देता।
उदाहरण: Iterating over HashMap
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Import java.util.Map so it can be used by its short name
import java.util.Map;
// 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 map
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
for (Map.Entry<String, Integer> entry : map.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
Updating Values
पहले से मौजूद key के साथ put() call करना इसकी previous value overwrite करता है और पुरानी वाली return करता है, जो एक call में दोनों एक value update करने और यह जांचने का एक convenient तरीका है कि यह पहले क्या था।
उदाहरण: Updating Values
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
Integer old = map.put("apple", 10); // overwrites, returns old value
System.out.println(old);
System.out.println(map.get("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Useful HashMap Methods
Basic get/put से आगे, HashMap null checks avoid करने के लिए getOrDefault(), overwriting avoid करने के लिए putIfAbsent(), और एक step में एक मौजूदा value को एक नई के साथ combine करने के लिए merge() प्रदान करता है — सभी common update patterns के लिए boilerplate घटाते हैं।
उदाहरण: Useful HashMap Methods
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
System.out.println(map.getOrDefault("banana", 0));
map.putIfAbsent("apple", 99); // won't overwrite
map.merge("apple", 2, Integer::sum);
System.out.println(map.get("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
HashMapinsertion order रखेगा। - एक missing key पर
map.get(key)call करना औरnullउपयोग करना, जो unboxing करते समयNullPointerExceptionका कारण बन सकता है। - इस पर iterate करते समय map modify करना, जो
ConcurrentModificationExceptionthrow करता है।
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