

Java 8 has introduced good performance improvement in case of too many hash collisions.īefore Java 7 If two objects have the same hashcode and are not equal, then both will be stored at the same bucket with the help of the singly linked list. To understand this change, you need to understand How HashMap works internally. This topic deserves a separate post, so I have written a complete tutorial on How HashMap works in java. The disadvantage of using Collections.synchronizedMap() is that it locks whole hashmap object and which may cause performance issue but ConcurrentHashMap only locks part of the map and performs quite well in a multithreaded environment. incrementTime() should be also synchronized otherwise there will be atomicity issues.Īs you can see, we are getting the correct output after using Collections.synchronizedMap() and making incrementTime synchronized. We can use Collections.synchronizedMap() to make all operations of HashMap thread-safe and make incrementTime() method synchronized to solve above issue. We can solve this thread safety issue in two ways: It is different from the last execution and this is due to thread-safety issues in HashMap. Time for Counter1: 98 Time for Counter2: 197 Time for Counter1: 100 Time for Counter2: 200īecause we have submitted task 100 times and in each task execution, it calls incrementTime() and increases the time by 1. Time for Counter1: 95 Time for Counter2: 195 Let’s run the program and check the output: We have created a task which increments time for both counters by 1 and we are using ExecuterService to submit it 100 times. I have put two entries in map with key as counter1 and counter2 and value as time 0 and 100 respectively. You can store custom objects as Key in HashMap but you should implement hashcode and equals method, otherwise, it may not work as expected. = Iterating over HashMap with foreach and lambda: Anchit → India Andy → USA Roy → Germary Mary → France = Iterating over HashMap using keyset() with foreach loop: Anchit → India Andy → USA Roy → Germary Mary → France = Iterating over HashMap’s keyset() with foreach and lambda: Anchit → India Andy → USA Roy → Germary Mary → France = Iterating over HashMap’s entrySet with iterator Anchit → India Andy → USA Roy → Germary Mary → France = Iterating over HashMap’s entrySet with foreach and lambda Anchit → India Andy → USA Roy → Germary Mary → France = Iterating over HashMap’s entrySet with foreach loop Anchit → India Andy → USA Roy → Germary Mary → France Storing Custom objects as Key We can use put() method to add entries to HashMap. Public HashMap(Map m): This constructor is used when you want to create HashMap from some other Map such as TreeMap or LinkedHashMap.

In most of the scenarios, you should avoid using this constructor unless you are sure about this as load factor 0.75 provides a good tradeoff between time and space.

Public HashMap(int initialCapacity,float loadFactor): This constructor is used to specify the initial capacity of the HashMap and load factor. Public HashMap(int initialCapacity): This constructor is used to specify the initial capacity of HashMap and default load factor 0.75. It creates an empty HashMap with a default initial capacity of 16 and load factor 0.75. Public HashMap(): This is the default constructor and used mostly. Did you notice HashMap implements Map interface even if AbstractMap already implements it? Yes, Just to make things more obvious, HashMap implements Map interface again and there is nothing wrong in implementing interface again.You don’t have to go through class Hierarchy to find it out that HashMap implements Map interface.
