A Map is an interface in Java that represents a collection of key-value pairs where each key is unique. It is part of the Java Collections Framework. Common implementations of the Map interface include HashMap, LinkedHashMap, and TreeMap.
The following example demonstrates how to use a HashMap to store and retrieve values using keys:
import java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
// Creating a HashMap
Map map = new HashMap<>();
// Adding key-value pairs
map.put("Apple", 10);
map.put("Banana", 20);
map.put("Orange", 15);
// Retrieving a value
int appleCount = map.get("Apple");
System.out.println("Apple count: " + appleCount);
// Iterating through the map
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?