HashMap in Java

HashMap & its feature ::1. HashMap class is an important class of Collection framework.2. HashMap extends generic AbstractMap.3. HashMap implements generic Map, cloneable and java.io.Serializable interface.4. HashMap is a Hash table based implementation of the Map interface.This implementation provides all of the optional map operations, and permits null values and the null key. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.For Detail understanding Please see How does a HashMap work internally ?5. HashMap is unsynchronized i.e. it is not thread safe.But, it can be made synchronized explicitly like below

6.HashMap can accept only one null key and multiple null values.7. HashMap implementation provides average O(1) time cost for the basic operations put(), get(),remove() and containsKey().8.HashMap uses Iterator interface to traverse the elements.While traversing the elements using iterator if the structure of the HashMap is modified using remove method or re hashing of hashMap is done,iterator will throw ConcurrentModificationException.So iterators for HashMap are fail-fast.While iterating you can add new elements without getting ConcurrentModificationException.

Sample Program ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

packagecom.ai1tutorial.collection.map;

import java.util.HashMap;

import java.util.Map;

publicclassHashMapDemo{

publicstaticvoidmain(String[]args){

Map<String,String>hashMap=newHashMap<String,String>();

hashMap.put("Ajay","Cricket");

hashMap.put("Bijay","Cricket");

hashMap.put("Jyoti","Badminton");

hashMap.put("Trupti","BabySitting");

hashMap.put("Deepti","Managing");

hashMap.put("Asha","Teaching");

hashMap.put("Goutam","Managing");

hashMap.put("Papu",null);

hashMap.put(null,null);

for(Map.Entry<String,String>entry:hashMap.entrySet()){

System.out.println(entry.getKey()+" "+entry.getValue());

}

}

}

Output ::

1

2

3

4

5

6

7

8

9

nullnull

Papu null

Asha Teaching

Bijay Cricket

Goutam Managing

Deepti Managing

Trupti BabySitting

Ajay Cricket

Jyoti Badminton

Now let us see an example of ConcurrentModificationException.In the below program while iterating the HashMap,i am modifying the structure of the HashMap by removing an element.So I am getting ConcurrentModificationException.Example of ConcurrentModificationException ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

packagecom.ai1tutorial.collection.map;

import java.util.HashMap;

import java.util.Map;

publicclassHashMapDemo{

publicstaticvoidmain(String[]args){

Map<String,String>hashMap=newHashMap<String,String>();

hashMap.put("Ajay","Cricket");

hashMap.put("Bijay","Cricket");

hashMap.put("Jyoti","Badminton");

hashMap.put("Trupti","BabySitting");

hashMap.put("Deepti","Managing");

hashMap.put("Asha","Teaching");

hashMap.put("Goutam","Managing");

hashMap.put("Papu",null);

hashMap.put(null,null);

for(Map.Entry<String,String>entry:hashMap.entrySet()){

System.out.println(entry.getKey()+" "+entry.getValue());

hashMap.remove("Goutam","Managing");

}

}

}

Output ::

1

2

3

4

5

6

nullnull

Exception inthread"main"java.util.ConcurrentModificationException

at java.util.HashMap$HashIterator.nextNode(Unknown Source)

at java.util.HashMap$EntryIterator.next(Unknown Source)

at java.util.HashMap$EntryIterator.next(Unknown Source)

at com.ai1tutorial.collection.map.HashMapDemo.main(HashMapDemo.java:18)

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.