Java Programming Handbook

    Understanding Map Interface in Java

    The Map interface in Java is a part of the Java Collection Framework (JCF) but is not a part of the Collection interface. A Map is used to store key-value pairs, where each key is unique, and each key maps to at most one value.

    In this blog, we will explore the Map interface, its key methods, and its implementations: HashMap, LinkedHashMap, TreeMap, and Hashtable.

    What is the Map Interface?#

    The Map interface is present in the java.util package and is used to store data in the form of key-value pairs.

    public interface Map<K, V> { // Interface definition }

    Key Features of Map#

    • Stores Key-Value Pairs: Each key is unique and maps to a single value.
    • Efficient Lookup: Searching for a value by key is fast.
    • No Duplicate Keys: A key can map to only one value at a time.
    • Allows Null Keys and Values (depending on the implementation).

    Key Methods of Map Interface#

    MethodDescription
    put(K key, V value)Inserts a key-value pair into the map.
    get(Object key)Retrieves the value associated with a key.
    remove(Object key)Removes the key-value pair from the map.
    containsKey(Object key)Checks if a key is present in the map.
    containsValue(Object value)Checks if a value is present in the map.
    size()Returns the number of key-value pairs in the map.
    clear()Removes all elements from the map.

    Implementations of Map Interface

    1. HashMap (Unordered & Fast)#

    Characteristics:#

    • Uses a HashTable internally.
    • No guarantee of insertion order.
    • Allows one null key and multiple null values.
    • Fast access (O(1) time complexity for put, get, remove).

    Example:#

    import java.util.*; public class HashMapExample { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "Apple"); map.put(2, "Banana"); map.put(3, "Mango"); map.put(1, "Orange"); // Overwrites existing value System.out.println("HashMap: " + map); } }

    Output:

    HashMap: {1=Orange, 2=Banana, 3=Mango}

    Note: Order may vary since HashMap does not maintain insertion order.

    2. LinkedHashMap (Ordered & Fast)#

    Characteristics:#

    • Extends HashMap but maintains insertion order.
    • Uses a doubly linked list alongside a HashTable.
    • Faster than TreeMap but slightly slower than HashMap.

    Example:#

    import java.util.*; public class LinkedHashMapExample { public static void main(String[] args) { Map<Integer, String> map = new LinkedHashMap<>(); map.put(1, "Apple"); map.put(2, "Banana"); map.put(3, "Mango"); System.out.println("LinkedHashMap: " + map); } }

    Output:

    LinkedHashMap: {1=Apple, 2=Banana, 3=Mango}

    Note: The order of elements remains the same as insertion order.

    3. TreeMap (Sorted Order)#

    Characteristics:#

    • Implements SortedMap interface.
    • Maintains elements in sorted (ascending) order.
    • Uses a Red-Black Tree (Self-balancing BST).
    • Slower than HashMap but allows sorted retrieval.

    Example:#

    import java.util.*; public class TreeMapExample { public static void main(String[] args) { Map<Integer, String> map = new TreeMap<>(); map.put(30, "Thirty"); map.put(10, "Ten"); map.put(20, "Twenty"); System.out.println("TreeMap: " + map); } }

    Output:

    TreeMap: {10=Ten, 20=Twenty, 30=Thirty}

    Note: Elements are sorted in ascending order of keys.

    4. Hashtable (Thread-Safe but Slow)#

    Characteristics:#

    • Synchronized and thread-safe.
    • Does not allow null keys or values.
    • Slower than HashMap due to synchronization overhead.

    Example:#

    import java.util.*; public class HashtableExample { public static void main(String[] args) { Map<Integer, String> map = new Hashtable<>(); map.put(1, "Apple"); map.put(2, "Banana"); map.put(3, "Mango"); System.out.println("Hashtable: " + map); } }

    Output:

    Hashtable: {1=Apple, 2=Banana, 3=Mango}

    Note: Unlike HashMap, it does not allow null keys or values.

    Choosing the Right Map Implementation#

    FeatureHashMapLinkedHashMapTreeMapHashtable
    Order Maintained?NoYes (Insertion Order)Yes (Sorted Order)No
    Speed (Put/Get/Delete)FastestFastSlowest (Balanced Tree)Slow (Thread-safe)
    Allows null?Yes (1 key, multiple values)Yes (1 key, multiple values)NoNo
    Thread-Safe?NoNoNoYes
    Internal StructureHashTableHashTable + Linked ListRed-Black TreeSynchronized HashTable

    Conclusion#

    In this blog, we explored the Map interface and its implementations: HashMap, LinkedHashMap, TreeMap, and Hashtable.

    • Use HashMap when fast lookup is required, and ordering is not important.
    • Use LinkedHashMap when maintaining insertion order is needed.
    • Use TreeMap when sorting is necessary but at the cost of performance.
    • Use Hashtable when thread-safety is required but at the cost of speed.

    In the upcoming blogs we will go deep into each of is implementation classes.

    Last updated on Apr 09, 2025