Java Map Interface
A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs.
- HashMap: HashMap stores the keys in a hash table. It uses the hashCode() method of the keys to retrieve their value efficiently. The benefit is that the storing and retrieving of the values by key have constant time. The disadvantage is that we lose the order of the insertion.
- TreeMap: TreeMap stores the key in a sorted tree structure. The benefit is that the key is always in sorted order but adding and checking of elements by the key takes longer time O(log n).
- Hashtable: Hashtable is an array of the list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key. It contains only unique elements. It does not allow null key or value. It is synchronized.
Useful Methods
Modifier and Type |
Method |
Description |
void |
clear() |
It removes all keys and values from the map. |
boolean |
isEmpty() |
It returns whether the map is empty. |
int |
size() |
It returns the number of entries (key/value pairs) in the map. |
V |
get(Object key) |
It returns the value mapped by key or null if none is mapped. |
V |
put(K key, V value) |
It adds or replaces key/value pair. Returns previous value or null. |
V |
remove(Object key) |
It removes and returns the value mapped to the key. Returns null if none. |
boolean |
containsKey(Object key) |
It returns whether the key is in the map. |
boolean |
containsValue(Object) |
It returns value is in map. |
Set<K> |
keySet() |
It returns set of all keys. |
Collection<V> |
values() |
It returns Collection of all values. |
Example
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class MapExample { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("key1", "INDIA"); map.put("key2", "CHINA"); map.put("Key3", "NEPAL"); String s = map.get("Key1"); // INDIA for (Entry<String, String> key : map.entrySet()) System.out.print(key.getValue() + ","); // INDIA,CHINA,NEPAL, } }
output:
INDIA,CHINA,NEPAL,
Example: TreeMap
import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MyClass { public static void main(String args[]) { /* This is how to declare TreeMap */ TreeMap<String, Integer> treemap = new TreeMap<>(); /* Adding elements to TreeMap */ treemap.put("A", 1); treemap.put("C", 3); treemap.put("B", 2); treemap.put("E", 5); treemap.put("D", 4); /* Display content using Iterator */ Set set = treemap.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); System.out.print("key is: " + mapentry.getKey() + " & Value is: "); System.out.println(mapentry.getValue()); } } }
Output:
key is: A & Value is: 1 key is: B & Value is: 2 key is: C & Value is: 3 key is: D & Value is: 4 key is: E & Value is: 5
Example: HashTable
import java.util.Hashtable; import java.util.Map.Entry; import java.util.Set; public class MyClass { public static void main(String[] arg) { // creating a hash table Hashtable<String,Integer> hashTable = new Hashtable<>(); hashTable.put("A", 1); hashTable.put("C", 3); hashTable.put("B", 2); hashTable.put("E", 5); hashTable.put("D", 4); // checking whether hash table h is empty or not if (hashTable.isEmpty()) System.out.println("yes hash table is empty"); // creating set view for hash table Set<Entry<String,Integer>> s = hashTable.entrySet(); // printing set entries System.out.println("set entries: " + s); // obtaining hash code System.out.println("hash code is: " + hashTable.hashCode()); // remove value for 2 from Hashtable h hashTable.remove("C"); // checking Hashtable h System.out.println("values after remove: " + hashTable); } }
Output:
set entries: [A=1, E=5, D=4, C=3, B=2] hash code is: 320 values after remove: {A=1, E=5, D=4, B=2}