Learn to create clone of a HashMap in Java. We will see the java programs to create shallow copy and deep copy of a HashMap. Read More : A guide to cloning in Java 1. Clone HashMap – shallow copy 1.1. HashMap clone() method The best way to create shallow clone of hashmap is to […]
Java HashMap is not synchronized by default. If we add/remove key-value pairs from a HashMap in a concurrent application where multiple threads are adding and removing pairs, we may end up having inconsistent state of the map. Learn to synchronize hashmap and ConcurrentHashMap in Java. 1. Synchronize HashMap – ConcurrentHashMap Our first choice should always […]
Learn to merge two hashmaps in Java. We will learn to join hashmaps in two cases – ignore duplicate keys (overwrite values) or handle duplicate keys using Java 8. 1. Merge two hashmaps – ignore duplicate keys This one is simple solution. Simply use HashMap.putAll(HashMap) method which copies all of the mappings from the second […]
Learn to compare two hashmaps in Java by keys, values and key-value pairs. Also learn to compare while allowing or restricting duplicate values. 1. Compare hashmap for same key-values – HashMap.equals() By default, HashMap.equals() method compares two hashmaps by key-value pairs. It means both hashmap instances must have exactly same key-value pairs and both must […]
Can we use an object as a key for a hashmap in java? This is a very popular interview question indeed. It is asked immediately after “How HashMap works?”. Lets make a reasoning around user defined object as key in hashmap in java. 1. The contract between hashCode() and equals() The very basic need for […]
We have already learned about how HashMap in java works internally. If my last similar post, I tried to compare different “for loop” flavors available in java. These studies usually help in setting up best practices for your next project. I this post, I decided to compare traversal in hash map in java. HashMap is […]
Learn how hashmap works internally in java with example. Learn the hashmap internal implementation analysis, collision resolution and Java 8 hashmap update.