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

Java का HashMap

एक HashMap एक key और एक value के pairs store करता है, नाम से एक number ढूंढने वाली एक phone book जैसा। Key से चीज़ें ढूंढना बहुत तेज़ है।
Syntax
java
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

java
// 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"));
	}
}

Removing and Clearing Elements

remove(key) key से एक single mapping delete करता है, जबकि clear() पूरा map एक बार में खाली करता है — दोनों operations map की size को तुरंत invalidate करते हैं बिना इसे rebuild करने की ज़रूरत के।

उदाहरण: Removing and Clearing Elements

java
// 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());
	}
}

Iterating over HashMap

एक HashMap को iterate करने का आमतौर पर मतलब है key और value दोनों एक साथ पाने के लिए entrySet() पर loop करना, या जब आपको सिर्फ एक side चाहिए तो keySet()/values() — HashMap entries किस order में वापस आती हैं इसकी कोई guarantee नहीं देता।

उदाहरण: Iterating over HashMap

java
// 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());
		}
	}
}

Updating Values

पहले से मौजूद key के साथ put() call करना इसकी previous value overwrite करता है और पुरानी वाली return करता है, जो एक call में दोनों एक value update करने और यह जांचने का एक convenient तरीका है कि यह पहले क्या था।

उदाहरण: Updating Values

java
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"));
	}
}

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

java
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"));
	}
}
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. यह उम्मीद करना कि HashMap insertion order रखेगा।
  2. एक missing key पर map.get(key) call करना और null उपयोग करना, जो unboxing करते समय NullPointerException का कारण बन सकता है।
  3. इस पर iterate करते समय map modify करना, जो ConcurrentModificationException throw करता है।

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.