logo
Java Maps
Map
1. Map is a child interface of collection.
2. Up to know we are working with single object and single value where as in the map collections we are working with two objects and two elements.
3. The main purpose of the collection is to compare the key value pairs and to perform necessary operation.
4. The key and value pairs we can call it as map Entry.
5. Both keys and values are objects only.
6. In entire collection keys can't be duplicated but values can be duplicate.
Java Images
HashMap
1. It used to hold key value pairs
2. Underlying data Structure is HashTable.
3. Duplicate keys are not allowed but values can be duplicated.
4. Insertion order is not preserved.
5. Null is allowed for key (only once)and allows for values any number of times.
6. Every method is non-synchronized so multiple Threads are operate at a time hence permanence is high.
Creating HashMap object
a. HashMap hm = new HashMap();
Here, to store String objects as keys and integer objects as its values, we create the HashMap object as shown above. We did not mention any capacity for the HashMap. Hence, the default capacity of this HashMap will be taken as 16 and the load factor as 0.75. Load factor represents at what level this HashMap capacity should be doubled.
For example: capacity * load factor = 16 * 0.75
= 12.
This represents that after storing the 12th key-value pair into the HashMap, its capacity will become 32.
b. HashMap hm = new HashMap(60);
The preceding statement creates a HashMap object (hm) which can be used to store String type keys and Integer type values. The initial capacity of HashMap object (hm) is declared as 60.
c. HashMap hm = new HashMap(60, 0.5);
The preceding statement creates a HashMap object (hm) which can be used to store String type keys and Integer type values. The initial capacity of HashMap object (hm) is declared as 60 and the load factor as 0.5.
Map methods
1) value put(key, value): This method stores key-value pair into the HashMap object.

2) value get(key): This method returns the corresponding value when key is given. If the key does not have a value associated with it, then it returns null.

3) Set keyset(): This method, when applied on HashMap converts it into a Set where only keys will be stored.

4) Collection values(): This method, when applied on HashMap object returns all the values of the HashMap into a Collection object.

5) value remove(key): This method removes the key and corresponding value from the HashMap.

6) void clear(): This method removes all the key-value pairs from the map.

7) boolean isEmpty(): This method returns true if there are no key-value pairs in the HashMap.

8) int size(): This method returns the number of key-value pairs in the HashMap.

HashMap program
import java.util.*; 
class Test 
{ 
public static void main(String[] args) 
{ 
HashMap h=new HashMap(); 
h.put("hi",100); 
h.put("bye",100); 
h.put("a",100); 
System.out.println(h);
Set s=h.keySet(); 
System.out.println(s); 
Collection c=h.values(); 
System.out.println(c); 
Set s1=h.entrySet(); 
System.out.println(s1); 
Iterator itr=s1.iterator(); 
while (itr.hasNext()) 
{ 
Map.Entry m1=(Map.Entry)itr.next(); 
System.out.println(m1.getKey()+"------"+m1.getValue());
if (m1.getKey().equals("hi")) 
{ 
m1.setValue("ok"); 
} 
} 
System.out.println(s1); 
}
}
Output :
{a=100, bye=100, hi=100}
[a, bye, hi]
[100, 100, 100]
[a=100, bye=100, hi=100]
a------100
bye------100
hi------100
[a=100, bye=100, hi=ok]