• Map in Java | Java Tutorial

    Java Map is a part of collections framework. The Map interface is based on key value pair. It maps unique keys to values. The Map interface is not a subtype of the Collection interface. The Map interface acts similar to Collections but a bit different from the rest of the collection types. It can’t contain duplicate keys however duplicate values are allowed.

    Map in Java

    There are two interfaces for implementing Map in Java.
    1. Map
    2. SortedMap

    There are three classes of Map in Java are
    1. HashMap
    2. Hashtable
    3. TreeMap
    4. ConcurrentHashMap
    5. LinkedHashMap.

    A Map cannot contain duplicate keys and each key can map to at most one value. HashMap and LinkedHashMap allow null key and null value but TreeMap doesn’t allow null key and null value. HashMap has no predictable order but LinkedHashMap TreeMap have predictable order.

    HashMap: HashMap implements all of the Map operations and allows null values and one null key. HashMap does not maintain an order of its key-value elements. Therefore, consider to use a HashMap when order does not matter and nulls are acceptable.

    Check detailed post on HashMap

    LinkedHashMap: LinkedHashMap is the implementation of Map, it inherits HashMap class. It allows null values and null key. It maintains insertion order. So consider using a LinkedHashMap when you want a Map with its key-value pairs are sorted by their insertion order.

    Check detailed post on LinkedHashMap

    TreeMap: TreeMap is the implementation of Map and SortedMap. It maintains ascending order. It doesn’t allow nulls. So consider using a TreeMap when you want a Map sorts its key-value pairs by the natural order of the keys.

    Check detailed post on TreeMap

    Points to remember:

    • Map doesn’t allow duplicate keys, but it allows duplicate values.
    • HashMap and LinkedHashMap allows null keys and null values but TreeMap doesn’t allow any null key or value.
    • Map can’t be traversed so you need to convert it into Set using keySet() or entrySet() method.

    The Map interface includes methods for basic operations (such as put, get, remove, containsKey, containsValue, size, and empty), bulk operations (such as putAll and clear), and collection views (such as keySet, entrySet, and values).

    Methods in Map Interface:

    • public Object put(Object key, Object value): This method is used to insert an entry in the map.
    • public void putAll(Map m): This method is used to insert specified map in this map.
    • public Object remove(Object k): This method is used to delete an entry whose key equals k.
    • public Object get(Object k):This method is used to return the value associated with the key k.
    • public boolean containsKey(Object k): This method returns true if the invoking map contains k as key. Otherwise returns false.
    • public Set keySet(): This method is used to return a Set that contains the keys in the invoking map. This method
    • provides a set-view of the keys in the invoking map.
    • public Set entrySet(): This method is used to return a Set that contains the entries in the map. The set contains objects of type Map.Entry. This method provides a set-view of the invoking map.

    Collection Views

    The collection view methods allow a Map to be viewed as a Collection in the following ways.

    1. keySet – the Set of keys contained in the Map
    2. values – Collection of values contained in the Map. This Collection is not a Set, because multiple keys can map to the same value
    3. entrySet – the Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map. Entry, the type of the elements in this Set.

    Learn Collection in Java

    Map.Entry Interface:

    Entry is the sub interface of Map. We could access it by Map.Entry. It provides methods to get key and value.

    Methods of Map.Entry interface:

    • Object getKey() – It is used to obtain key.
    • Object getValue() – It is used to obtain value.

    Sample Program Using Map.Entry:

    Sample Program:

    Java Tutorial

    Sharing is caring.

    Share on facebook
    Share on twitter
    Share on linkedin

    Like This Post?

    We have a lot more where that came from?

    We only send really good stuff occasionally, promise.

    Rajkumar SM

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Share on facebook
    Share on google
    Share on twitter
    Share on linkedin
    Share on whatsapp

    Recent Posts:

    Scroll to Top
    API Testing eBook