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

Java का Map Interface

Map interface unique keys का एक collection represent करता है, हर एक exactly एक value को map करते हुए, इसे एक position के बजाय एक identifier से data lookup करने के लिए go-to structure बनाते हुए।
Syntax
java
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?

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

Map Implementations

HashMap, LinkedHashMap, और TreeMap Map के तीन standard implementations हैं: HashMap कोई ordering guarantee के बिना सबसे तेज़ lookups प्रदान करता है, LinkedHashMap insertion order संरक्षित रखता है, और TreeMap entries को key से sorted रखता है।

उदाहरण: Map Implementations

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

Key-Value Pairs

एक Map data को pairs के रूप में store करता है, जहां put एक key को एक value के साथ associate करता है और get बाद में उसी key का उपयोग करके वह value retrieve करता है -- एक मौजूदा key के तहत एक नई value डालना पुरानी value को overwrite करता है एक दूसरी entry बनाने के बजाय।

उदाहरण: Key-Value Pairs

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

Common Map Methods

Map interface put, get, remove, containsKey, और isEmpty जैसे methods define करता है जो हर implementation में consistently behave करते हैं, इसलिए HashMap, TreeMap, और LinkedHashMap के बीच switch करना यह नहीं बदलता कि map कैसे उपयोग होता है।

उदाहरण: Common Map Methods

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

Iterating a Map

एक Map तीन तरीकों से iterate किया जा सकता है: keySet सिर्फ keys return करता है, values सिर्फ values return करता है, और entrySet दोनों को एक साथ Map.Entry objects के रूप में return करता है, जो एक map की पूरी contents पर loop करने का सबसे आम तरीका है।

उदाहरण: Iterating a Map

java
// 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());
		}
	}
}
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. एक missing key पर get() call करना और null result उपयोग करना, जो NullPointerException throw करता है।
  2. यह उम्मीद करना कि duplicate keys रखी जाएंगी, जब put() पुरानी value replace करता है।
  3. एक mutable object को key के रूप में उपयोग करना और इसे insertion के बाद बदलना, इसलिए यह अब नहीं ढूंढा जा सकता।

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.