HashMap
1.ConcurrentModificationException
-1.Concept of hashing
-2.Collision resolution in HashMap
-3.Use of equals () and hashCode () and there importance in HashMap?
-4.Benefit of immutable object?
-5.Race condition on HashMap in Java
-6.Resizing of Java HashMap
Step1.5:applying hashing on that hashcode
Ste2: stored in next node of linked list
Map.Entry object
Important 0:
How null key is handled in HashMap? Since equals() and hashCode() are used to store and retrieve values, how does it work in case of null key?
Null key is handled specially in HashMap, there are two separate method for that putForNullKey(V value) and getForNullKey(). Later is offloaded version of get() to look up null keys. Null keys always map to index 0. This null case is split out into separate methods for the sake of performance in the two most commonly used operations (get and put), but incorporated with conditionals in others. In short, equals() and hashcode() method are not used in case of null keys in HashMap.
here is how nulls are retreived from HashMap
private V getForNullKey() {
// Case1: Empty
if (size == 0) { return null; }
// Case2: search table[0] for key == null for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; }
// Case3: Not Found, just return null return null; }
used HashMap as cache in electronic trading application I have worked
A cache is an area of local memory that holds a copy of frequently accessed data that is otherwise expensive to get or compute. Examples of such data include
1.a result of a query to a database,
2.a disk file or a report.
Important 1:
HashMap Changes in JDK 1.7 and JDK 1.8?
Problem1: which reduce memory consumption. Due to this empty Map are lazily initialized and will cost you less memory. Earlier, when you create HashMap e.g. new HashMap() it automatically creates array of default length e.g. 16. After some research, Java team founds that most of this Map are temporary and never use that many elements, and only end up wasting memory.
Problem2: Since a poor hash function e.g. which always return location of same bucket, can turn a HashMap into linked list, i.e. converting get() method to perform in O(n) instead of O(1) and someone can take advantage of this fact, Java now internally replace linked list to a binary tree once certain threshold is breached. This ensures performance or order O(log(n)) even in worst case where hash function is not distributing keys properly.
Important 1:
Do you Know how HashMap works in Java or How does get () method of HashMap works in Java ?
HashMap works on principle of hashing, we have put(key, value) and get(key) method for storing and retrieving Objects from HashMap. When we pass Key and Value object to put() method on Java HashMap, 1.HashMap implementation calls hashCode method on Key object and
2.applies returned hashcode into its own hashing function to find a bucket location for storing Entry object,
3.important point to mention is that HashMap in Java stores both key and value object as Map.Entry in bucket which is essential to understand the retrieving logic.
Important 2:
What will happen if two different objects have same hashcode?
2.Since hashcode is same, bucket location would be same and collision will occur in HashMap, Since HashMap use LinkedList to store object, this entry (object of Map.Entry comprise key and value ) will be stored in LinkedList.
Important3:
How will you retrieve Value object if two Keys will have same hashcode?
1. call get() method and HashMap uses Key Object's hashcode to find out bucket location
2.we will call keys.equals() method to identify correct node in LinkedList and return associated value object for that key in Java HashMap
4,Immutability also allows caching there hashcode of different keys which makes overall retrieval process very fast and suggest that String and various wrapper classes e.g. Integer very good keys in Java HashMap.
Important4:
What happens On HashMap in Java if the size of the HashMap exceeds a given threshold defined by load factor ?
1.if load factor is .75 it will act to re-size the map once it filled 75%
2.Java HashMap re-size itself by creating a new bucket array of size twice of previous size of HashMap ,
3.and then start putting every old element into that new bucket array.
4.This process is called rehashing because it also applies hash function to find new bucket location
Important5:
do you see any problem with resizing of HashMap in Java
1.multiple thread accessing the Java HashMap and potentially looking for race condition on HashMap in Java
2.potential race condition exists while resizing HashMap in Java, if two thread at the same time found that now HashMap needs resizing and they both try to resizing
3.on the process of resizing of HashMap in Java , the element in bucket which is stored in linked list get reversed in order during there migration to new bucket because Java HashMap doesn't append the new element at tail instead it append new element at head to avoid tail traversing. If race condition happens then you will end up with an infinite loop
Important 6:
Why String, Integer and other wrapper classes are considered good keys ?
2.Immutabiility is required, in order to prevent changes on fields used to calculate hashCode() because if key object return different hashCode during insertion and retrieval than it won't be possible to get object fromHashMap
3.Immutability is best as it offers other advantages as well like thread-safety, If you can keep your hashCode same by only making certain fields final, then you go for that as well
4.Since equals() and hashCode() method is used during reterival of value object from HashMap, its important that key object correctly override these methods and follow contact.
5. Ofcourse you can use any Object as key in Java HashMap provided it follows equals and hashCode contract and its hashCode should not vary once the object is inserted into Map.If custom object is Immutable than this will be already taken care because you can not change it once created
Reference:
|