Map Interface in Java
Map Interface in Java
key-value pairs. Unlike other collection types like List or Set , Map does not extend
the Collection interface.
HashMap Fast and allows one null key and multiple null values; no
ordering.
🧪 Basic Example:
java Copy Edit
import java.util.*; public class MapExample { public static void main(String[] args) {
Map<String, Integer> marks = new HashMap<>(); // Add elements marks.put("Math", 90);
marks.put("Science", 85); marks.put("English", 92); // Retrieve value
System.out.println("Math Marks: " + marks.get("Math")); // Check key presence
System.out.println("Contains 'Science'? " + marks.containsKey("Science")); // Loop
through entries for (Map.Entry<String, Integer> entry : marks.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue()); } } }
Method Description