The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit different from the rest of the collection types.
Few characteristics of the Map Interface are:
- A Map cannot contain duplicate keys and each key can map to at most one value. Some implementations allow null key and null value like the HashMap and LinkedHashMap, but some do not like the TreeMap.
- The order of a map depends on specific implementations, e.g TreeMap and LinkedHashMap have predictable order, while HashMap does not.
- There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap, TreeMap and LinkedHashMap.
Why and When to use Maps?
Maps are perfect to use for key-value association mapping such as dictionaries. The maps are used to perform lookups by keys or when someone wants to retrieve and update elements by keys. Some examples are:
- A map of error codes and their descriptions.
- A map of zip codes and cities.
- A map of managers and employees. Each manager (key) is associated with a list of employees (value) he manages.
- A map of classes and students. Each class (key) is associated with a list of students (value).
Methods in Map Interface:
- public Object put(Object key, Object value): This method is used to insert an entry in this map.
- public void putAll(Map map): This method is used to insert the specified map in this map.
- public Object remove(Object key): This method is used to delete an entry for the specified key.
- public Object get(Object key):This method is used to return the value for the specified key.
- public boolean containsKey(Object key): This method is used to search the specified key from this map.
- public Set keySet(): This method is used to return the Set view containing all the keys.
- public Set entrySet(): This method is used to return the Set view containing all the keys and values.
a:100 b:200 c:300 d:400
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Map.Entry interface in Java with example
- Nested Interface in Java
- Java Interface methods
- Externalizable interface in Java
- Evolution of interface in Java
- BinaryOperator Interface in Java
Improved By : Chinmoy Lenka