Map-Interface-overview
The map interface is a part of the Java Collection Framework, but it does not inherit the collection interface.
Indications / Symbols
- << >> : Interface
- < > : Class
- ----------> : Implements
Features
- A map is a pre-defined Interface present in Java.util package and introduced from JDK1.2.
- A map is a collection of key-value pairs. One key and value together is called an entry.
- A key can be any type of object and value can be any type of object, but the key must be unique.
- The value can be retrieved based on the key.
Implementation Classes of Map
The map interface has several implementation classes, the implementation classes of Map Interface are:- HashMap
- LinkedHashMap
- TreeMap
- Hashtable
Class Name | Ordering | Allows Null Keys/Values ? | Thread-Safe ? |
---|---|---|---|
HashMap<K, V> | No specific order | Yes(1 null key, many null values) | No |
LinkedHasMap
<K, V>
| Insertion order maintained | Yes(1 null key, many null values) | No |
TreeMap
<K, V>
| Sorted in natural order of keys | No null keys, Null values | No |
Hashtable
<K, V>
| No specific order | No null keys or values | Yes |
Common Methods of the map Interface
Method | Description |
---|---|
V put(K key, V value) | Insert a key-value pair into the map. |
V get(object key) | Returns the value associated with the specified key. |
boolean containsKey(object key) | Checks if a given key is present in the map. |
boolean containsValue(object value) | Checks if a given value is present in the map. |
Set<K> keySet() | Returns a set of all keys in the map. |
Collection<V> values() | Returns a collection of all values in the map. |
int size() | Returns the number of key-value pairs. |
V remove(object key) | Removes the entry associated with the give key. |
boolean isEmpty() | Checks if teh map is empty. |
Example:
package com.quipoin;
import java.util.HashMap;
public class Demo {
public static void main(String[] args) {
HashMap hm=new HashMap<>();
hm.put(10, "Java");
hm.put("sql", 12);
hm.put(5.6, 89);
System.out.println(hm);
System.out.println("------------------");
//get() method is used to get the value based on the key specified.
System.out.println(hm.get(10)); //Java
System.out.println(hm.get(5.6)); //89
System.out.println("------------------");
//containsKey() method is used to check if the specified key is present ornot.
System.out.println(hm.containsKey(10)); //true
System.out.println(hm.containsKey("SQL")); //false
System.out.println("------------------");
//containsValue() method is used to check if the value is present or not
System.out.println(hm.containsValue(12)); //true
System.out.println(hm.containsValue("java")); //false
System.out.println("------------------");
//size() method is used to find number of entries present in Map.
System.out.println(hm.size());
System.out.println("------------------");
//keSet() method returns set of keys from the map;
System.out.println(hm.keySet());
}
}
Output:
{5.6=89, 10=Java, sql=12}
------------------
Java
89
------------------
true
false
------------------
true
false
------------------
3
------------------
[5.6, 10, sql]
Comparison of Different Map Implementations
1. HashMap<K, V>
- Unordered collection of key-value pairs.
- Fastest performance for basic operations.
- Allows one null key and multiple null values.
- Not thread-safe (Use Collections.synchronizedMap() for thread safety).
HashMap map = new HashMap<>();
map.put(101, "Alice");
map.put(102, "Bob");
System.out.println(map);
2. LinkedHashMap<K, V>
- Maintains insertion order.
- Slower than HashMap, but predictable iteration order.
- Allows one null key and multiple null values.
Example:
LinkedHashMapmap = new LinkedHashMap<>(); map.put(101, "Alice"); map.put(102, "Bob"); System.out.println(map);
3. TreeMap<K, V>
- Maintains sorted order (ascending by default).
- Does not allow null keys, but allows multiple null values.
- Slower than HashMap and LinkedHashMap due to sorting overhead.
TreeMap map = new TreeMap<>();
map.put(102, "Bob");
map.put(101, "Alice");
System.out.println(map); // Output: {101=Alice, 102=Bob}
- Thread-safe and synchronized.
- Does not allow null keys or values.
- Slower compared to other Map implementations.
Example:
Hashtabletable = new Hashtable<>(); table.put(101, "Alice"); table.put(102, "Bob"); System.out.println(table);
Two Minute Drill
- HashMap is the most commonly used Map implementation.
- LinkedHashMap maintains insertion order.
- TreeMap maintains sorted order but does not allow null keys.
- Hashtable is synchronized but has performance overhead.