http://www.journaldev.com/1162/java-multi-threading-concurrency-interview-questions-with-answers
Friday, July 24, 2015
Thursday, July 23, 2015
Setup-and-gotchas-of-the-hibernate-second-level-and-query-caches
http://blog.jhades.org/setup-and-gotchas-of-the-hibernate-second-level-and-query-caches/
concurrent hashmap
http://javarevisited.blogspot.sg/2011/04/difference-between-concurrenthashmap.html
How to use ConcurrentHashMap in Java - Example Tutorial and Working
ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java 1.5 as part of Java concurrency package. Prior to Java 1.5 if you need a Map implementation, which can be safely used in a concurrent and multi-threaded Java program, than, you only have Hashtable or synchronized Map because HashMap is not thread-safe. WithConcurrentHashMap, now you have better choice; because, not only it can be safely used in concurrent multi-threaded environment but also provides better performance over Hashtable and synchronizedMap. ConcurrentHashMap performs better than earlier two because it only locks a portion of Map, instead of whole Map, which is the case with Hashtable and synchronized Map. CHM allows concurred read operations and same time, maintains integrity by synchronizing write operations. We have seen basics of ConcurrentHashMap on Top 5 Java Concurrent Collections from JDK 5 and 6 and in this Java tutorial, we will learn :
Ø How ConcurrentHashMap works in Java or how it is implemented in Java.
Ø When to use ConcurrentHashMap in Java
Ø ConcurrentHashMap examples in Java
How ConcurrentHashMap is implemented in Java
ConcurrentHashMap is introduced as an alternative of Hashtable and provided all functions supported by Hashtable with additional feature called "concurrency level", which allows ConcurrentHashMap to partition Map. ConcurrentHashMap allows multiple readers to read concurrently without any blocking. This is achieved by partitioning Map into different parts based on concurrency level and locking only a portion of Map during updates. Default concurrency level is 16, and accordingly Map is divided into 16 part and each part is governed with different lock. This means, 16 thread can operate on Map simultaneously, until they are operating on different part of Map. This makes ConcurrentHashMap high performance despite keeping thread-safety intact. Though, it comes with caveat. Since update operations like put(), remove(),putAll() or clear() is not synchronized, concurrent retrieval may not reflect most recent change on Map.
In case of putAll() or clear(), which operates on whole Map, concurrent read may reflect insertion and removal of only some entries. Another important point to remember is iteration over CHM, Iterator returned by keySet of ConcurrentHashMap are weekly consistent and they only reflect state of ConcurrentHashMap and certain point and may not reflect any recent change. Iterator of ConcurrentHashMap's keySetarea also fail-safe and doesn’t throw ConcurrentModificationExceptoin..
Default concurrency level is 16 and can be changed, by providing a number which make sense and work for you while creating ConcurrentHashMap. Since concurrency level is used for internal sizing and indicate number of concurrent update without contention, so, if you just have few writers or thread to update Map keeping it low is much better. ConcurrentHashMap also uses ReentrantLock to internally lock its segments.
ConcurrentHashMap putifAbsent example in Java
ConcurrentHashMap examples are similar to Hashtable examples, we have seen earlier, but worth knowing is use of putIfAbsent() method. Many times we need to insert entry into Map, if its not present already, and we wrote following kind of code:
synchronized(map){
if (map.get(key) == null){
return map.put(key, value);
} else{
return map.get(key);
}
}
Though this code will work fine in HashMap and Hashtable, This won't work in ConcurrentHashMap; because, during put operation whole map is not locked, and while one thread is putting value, other thread's get() call can still return null which result in one thread overriding value inserted by other thread. Ofcourse, you can wrap whole code in synchronized block and make it thread-safe but that will only make your code single threaded. ConcurrentHashMap provides putIfAbsent(key, value) which does same thing but atomically and thus eliminates above race condition.
When to use ConcurrentHashMap in Java
ConcurrentHashMap is best suited when you have multiple readers and few writers. If writers outnumber reader, or writer is equal to reader, than performance of ConcurrentHashMap effectively reduces to synchronized map or Hashtable. Performance of CHM drops, because you got to lock all portion of Map, and effectively each reader will wait for another writer, operating on that portion of Map. ConcurrentHashMap is a good choice for caches, which can be initialized during application start up and later accessed my many request processing threads. As javadoc states, CHM is also a good replacement of Hashtable and should be used whenever possible, keeping in mind, that CHM provides slightly weeker form of synchronization than Hashtable.
Summary
Now we know What is ConcurrentHashMap in Java and when to use ConcurrentHashMap, it’s time to know and revise some important points about CHM in Java.
1. ConcurrentHashMap allows concurrent read and thread-safe update operation.
2. During update operation, ConcurrentHashMap only lock a portion of Map instead of whole Map.
3. Concurrent update is achieved by internally dividing Map into small portion which is defined by concurrency level.
4. Choose concurrency level carefully as a significant higher number can be waste of time and space and lower number may introduce thread contention in case writers over number concurrency level.
6. Since ConcurrentHashMap implementation doesn't lock whole Map, there is chance of read overlapping with update operations like put() and remove(). In that case result returned by get() method will reflect most recently completed operation from there start.
7. Iterator returned by ConcurrentHashMap is weekly consistent, fail safe and never throw ConcurrentModificationException. In Java.
8. ConcurrentHashMap doesn't allow null as key or value.
9. You can use ConcurrentHashMap in place of Hashtable but with caution as CHM doesn't lock whole Map.
10. During putAll() and clear() operations, concurrent read may only reflect insertion or deletion of some entries.
That’s all on What is ConcurrentHashMap in Java and when to use it. We have also seen little bit about internal working of ConcurrentHashMap and how it achieves it’s thread-safety and better performance over Hashtable and synchronized Map. Use ConcurrentHashMap in Java program, when there will be more reader than writers and it’s a good choice for creating cache in Java as well.
Related Java Concurrency and Collection Tutorials from this blog
Labels: core java , java collection tutorial
Location: United States
Read more: http://javarevisited.blogspot.com/2013/02/concurrenthashmap-in-java-example-tutorial-working.html#ixzz3gj5h1tdv
How HashMap works in Java
http://javarevisited.blogspot.sg/2011/04/difference-between-concurrenthashmap.html
HashMap in Java works on hashing principle. It is a data structure which allows us to store object and retrieve it in constant time O(1) provided we know the key. In hashing, hash functions are used to link key and value in HashMap. Objects are stored by calling put(key, value) method of HashMap and retrieved by calling get(key) method. When we call put method, hashcode() method of key object is called so that hash function of map can find a bucket location to store value object, which is actually index of internal array, known as table. HashMap internally store mapping in form of Map.Entry object which contains both key and value object. When you want to retrieve the object, you call get() method and again pass key object. This time again key object generate same hash code (it's mandatory for it to do so to retrieve object and that's why HashMap keys are immutable e.g. String) and we end up at same bucket location. If there is only one object then it is returned and that's your value object which you have stored earlier. Things get little tricky when collisions occurs. Since internal array of HashMap is of fixed size, and if you keep storing objects, at some point of time hash function will return same bucket location for two different keys, this is called collision in HashMap. In this case, a linked list is formed at that bucket location and new entry is stored as next node. If we try to retrieve object from this linked list, we need an extra check to search correct value, this is done by equals() method. Since each node contains an entry, HashMap keep comparing entry's key object with passed key using equals() and when it return true, Map returns corresponding value. Since searching in lined list is O(n) operation, in worst case hash collision reduce a map to linked list. This issue is recently addressed in Java 8 by replacing linked list to tree to search in O(logN) time. By the way, you can easily verify how HashMap work by looking at code of HashMap.java in your Eclipse IDE, if you know how to attach source code of JDK in Eclipse.
How HashMap works in Java or sometime how get method work in HashMap is a very common question on Java interviews now days. Almost everybody who worked in Java knows about HashMap, where to use HashMap and difference between Hashtable and HashMap then why this interview question becomes so special? Because of the depth it offers. It has become very popular Java interview question in almost any senior or mid-senior level Java interviews. Investment banks mostly prefer to ask this question and some time even ask you to implement your own HashMap based upon your coding aptitude. Introduction of ConcurrentHashMap and other concurrent collections has also made this questions as starting point to delve into more advanced feature. let's start the journey.
Interviewee will say we will call get() method and then HashMap uses Key Object's hashcode to find out bucket location and retrieves Value object but then you need to remind him that there are two Value objects are stored in same bucket , so they will say about traversal in LinkedList until we find the value object , then you ask how do you identify value object because you don't have value object to compare ,Until they know that HashMap stores both Key and Value in LinkedList node or as Map.Entry they won't be able to resolve this issue and will try and fail.
Now if you clear this entire Java HashMap interview, You will be surprised by this very interesting question "What happens On HashMap in Java if the size of the HashMap exceeds a given threshold defined by load factor ?". Until you know how HashMap works exactly you won't be able to answer this question. If the size of the Map exceeds a given threshold defined by load-factor e.g. if load factor is .75 it will act to re-size the map once it filled 75%. Similar to other collection classes like ArrayList, Java HashMap re-size itself by creating a new bucket array of size twice of previous size of HashMap , and then start putting every old element into that new bucket array. This process is called rehashing because it also applies hash function to find new bucket location.
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() {
HashMap in Java works on hashing principle. It is a data structure which allows us to store object and retrieve it in constant time O(1) provided we know the key. In hashing, hash functions are used to link key and value in HashMap. Objects are stored by calling put(key, value) method of HashMap and retrieved by calling get(key) method. When we call put method, hashcode() method of key object is called so that hash function of map can find a bucket location to store value object, which is actually index of internal array, known as table. HashMap internally store mapping in form of Map.Entry object which contains both key and value object. When you want to retrieve the object, you call get() method and again pass key object. This time again key object generate same hash code (it's mandatory for it to do so to retrieve object and that's why HashMap keys are immutable e.g. String) and we end up at same bucket location. If there is only one object then it is returned and that's your value object which you have stored earlier. Things get little tricky when collisions occurs. Since internal array of HashMap is of fixed size, and if you keep storing objects, at some point of time hash function will return same bucket location for two different keys, this is called collision in HashMap. In this case, a linked list is formed at that bucket location and new entry is stored as next node. If we try to retrieve object from this linked list, we need an extra check to search correct value, this is done by equals() method. Since each node contains an entry, HashMap keep comparing entry's key object with passed key using equals() and when it return true, Map returns corresponding value. Since searching in lined list is O(n) operation, in worst case hash collision reduce a map to linked list. This issue is recently addressed in Java 8 by replacing linked list to tree to search in O(logN) time. By the way, you can easily verify how HashMap work by looking at code of HashMap.java in your Eclipse IDE, if you know how to attach source code of JDK in Eclipse.
How HashMap works in Java or sometime how get method work in HashMap is a very common question on Java interviews now days. Almost everybody who worked in Java knows about HashMap, where to use HashMap and difference between Hashtable and HashMap then why this interview question becomes so special? Because of the depth it offers. It has become very popular Java interview question in almost any senior or mid-senior level Java interviews. Investment banks mostly prefer to ask this question and some time even ask you to implement your own HashMap based upon your coding aptitude. Introduction of ConcurrentHashMap and other concurrent collections has also made this questions as starting point to delve into more advanced feature. let's start the journey.
How HashMap Internally Works in Java
Questions start with simple statement :
Have you used HashMap before or What is HashMap? Why do you use it
Almost everybody answers this with yes and then interviewee keep talking about common facts about HashMap like HashMap accept null while Hashtable doesn't, HashMap is not synchronized, HashMap is fast and so on along with basics like its stores key and value pairs etc. This shows that person has used HashMap and quite familiar with the functionality it offers, but interview takes a sharp turn from here and next set of follow-up questions gets more detailed about fundamentals involved with HashMap in Java . Interviewer strike back with questions like :
Do you Know how HashMap works in Java or How does get () method of HashMap works in Java
And then you get answers like, I don't bother its standard Java API, you better look code on Java source or Open JDK; I can find it out in Google at any time etc. But some interviewee definitely answer this and will say HashMap works on principle of hashing, we have put(key, value) andget(key) method for storing and retrieving Objects from HashMap. When we pass Key and Value object to put() method on Java HashMap, HashMap implementation calls hashCode method on Key object and applies returned hashcode into its own hashing function to find a bucket location for storing Entry object, 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. If people fails to recognize this and say it only stores Value in the bucket they will fail to explain the retrieving logic of any object stored in Java HashMap . This answer is very much acceptable and does make sense that interviewee has fair bit of knowledge on how hashing works and how HashMap works in Java. But this is just start of story and confusion increases when you put interviewee on scenarios faced by Java developers on day by day basis. Next question could be about collision detection and collision resolution in Java HashMap e.g.
What will happen if two different objects have same hashcode?
Now from here onwards real confusion starts, Some time candidate will say that since hashcode is equal, both objects are equal and HashMap will throw exception or not store them again etc, Then you might want to remind them about equals() and hashCode() contract that two unequal object in Java can have same hash code. Some will give up at this point and few will move ahead and say "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. Great this answer make sense though there are many collision resolution methods available like linear probing and chaining, this is simplest and HashMap in Java does follow this. But story does not end here and interviewer asks
How will you retrieve Value object if two Keys will have same hashcode?
Interviewee will say we will call get() method and then HashMap uses Key Object's hashcode to find out bucket location and retrieves Value object but then you need to remind him that there are two Value objects are stored in same bucket , so they will say about traversal in LinkedList until we find the value object , then you ask how do you identify value object because you don't have value object to compare ,Until they know that HashMap stores both Key and Value in LinkedList node or as Map.Entry they won't be able to resolve this issue and will try and fail.
But those bunch of people who remember this key information will say that after finding bucket location , we will call keys.equals() method to identify correct node in LinkedList and return associated value object for that key in Java HashMap . Perfect this is the correct answer.
In many cases interviewee fails at this stage because they get confused between hashCode() and equals() or keys and values object in Java HashMap which is pretty obvious because they are dealing with the hashcode() in all previous questions and equals() come in picture only in case of retrieving value object from HashMap in Java. Some good developer point out here that using immutable, final object with proper equals() and hashcode() implementation would act as perfect Java HashMap keys and improve performance of Java HashMap by reducing collision. 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.
Now if you clear this entire Java HashMap interview, You will be surprised by this very interesting question "What happens On HashMap in Java if the size of the HashMap exceeds a given threshold defined by load factor ?". Until you know how HashMap works exactly you won't be able to answer this question. If the size of the Map exceeds a given threshold defined by load-factor e.g. if load factor is .75 it will act to re-size the map once it filled 75%. Similar to other collection classes like ArrayList, Java HashMap re-size itself by creating a new bucket array of size twice of previous size of HashMap , and then start putting every old element into that new bucket array. This process is called rehashing because it also applies hash function to find new bucket location.
If you manage to answer this question on HashMap in Java you will be greeted by "do you see any problem with resizing of HashMap in Java" , you might not be able to pick the context and then he will try to give you hint about multiple thread accessing the Java HashMap and potentially looking for race condition on HashMap in Java.
So the answer is Yes there is 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. 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. Though this point you can potentially argue that what the hell makes you think to use HashMap in multi-threaded environment to interviewer :)
Some more Hashtable and HashMap Questions
Few more question on HashMap in Java which is contributed by readers of Javarevisited blog :
1) Why String, Integer and other wrapper classes are considered good keys ?
String, Integer and other wrapper classes are natural candidates of HashMap key, and String is most frequently used key as well because String is immutable and final,and overrides equals and hashcode() method. Other wrapper class also shares similar property. 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 from HashMap. 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. 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. If unequal object return different hashcode than chances of collision will be less which subsequently improve performance of HashMap.
2) Can we use any custom object as key in HashMap ?
This is an extension of previous questions. 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.
3) Can we use ConcurrentHashMap in place of Hashtable ?
This is another question which getting popular due to increasing popularity of ConcurrentHashMap. Since we know Hashtable is synchronized but ConcurrentHashMap provides better concurrency by only locking portion of map determined by concurrency level. ConcurrentHashMap is certainly introduced as Hashtable and can be used in place of it but Hashtable provide stronger thread-safety than ConcurrentHashMap. See my post difference between Hashtable and ConcurrentHashMap for more details.
Personally, I like this question because of its depth and number of concept it touches indirectly, if you look at questions asked during interview this HashMap questions has verified
- Concept of hashing
- Collision resolution in HashMap
- Use of equals () and hashCode () and there importance in HashMap?
- Benefit of immutable object?
- Race condition on HashMap in Java
- Resizing of Java HashMap
Just to summarize here are the answers which does makes sense for above questions
How HashMap works in Java
HashMap works on principle of hashing, we have put() and get() method for storing and retrieving object form HashMap .When we pass an both key and value to put() method to store on HashMap , it uses key object hashcode() method to calculate hashcode and they by applying hashing on that hashcode it identifies bucket location for storing value object. While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list. AlsoHashMap stores both key and value tuple in every node of linked list in form of Map.Entry object.
What will happen if two different HashMap key objects have same hashcode?
They will be stored in same bucket but no next node of linked list. And keys equals () method will be used to identify correct key value pair in HashMap .
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() {
if (size == 0) { return null; } for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; } return null; }
In terms of usage Java HashMap is very versatile and I have mostly used HashMap as cache in electronic trading application I have worked . Since finance domain used Java heavily and due to performance reason we need caching HashMap and ConcurrentHashMap comes as very handy there. You can also check following articles form Javarevisited to learn more about HashMap and Hashtable in Java :
HashMap Changes in JDK 1.7 and JDK 1.8
There is some performance improvement done on HashMap and ArrayList from JDK 1.7, 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. Also, From JDK 1.8 onwards HashMap has introduced an improved strategy to deal with high collision rate. 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 true 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.
Related post:
Location: North America
127 comments :
- Anonymous said...
- This same question was asked to me during my interview with a Investment bank for Java FIX developer position. I like follow up question though :)
- February 3, 2011 at 4:01 AM
- Javin @ Tibco RV Tutorial said...
- Hi Sandeep,
Nice to see you here back. yes this is common core java interview question but when you ask cross question then half of developer fails. in most cases they forgot that hashmap stores both key and value in bucket.I have seen interviews where they think that only value is stored in bucket which leads to confusion when asking about duplicate hashcode.
Thanks
JAvin - February 4, 2011 at 5:21 AM
- Anonymous said...
- Hi Dude Can we use hashMap in multithreaded scenario ? is there any issue with that ?
- February 7, 2011 at 3:42 AM
- Gautam said...
- Good explanation. Some pictures and flowcharts will be good.
- February 7, 2011 at 9:55 AM
- Foudres said...
- Well it is obvious that the only mandatory things to store in a dictionnary/map is the key and the value.
Without any more hypothesis, this will resort to O(n) when retriving from the key.
But O(n) is very very bad.
So you start to think at a way to have better performance. You can think about an index. You use ordered type and construct a tree.
Problem is that not all types can be ordered. The hash allow you to treat any type like an ordered one with the exception of collisions. Thus you have O(log n), but could have O(n) in worst case if all entry have the same hash.
But really I found your interview question to be confusing. I can describe what is a dictionnary, what strategy is basically used for it, but I can't say how JAVA implement it. This is a complex question that need basically that you have read the source code and that would depend of the JVM implementation you use.
And if you are really interrested for this matter you now that depending of the exact algorythm you use you have very different performance depending of your exact needs :
map with lot of entry
map with few entries.
how to optimise the partitioning what ever the hash you are given...
For hash and map keys : of course you should use only immuable key. A dictionnary/map is a nonscence if your key is mutable as it is likely that the hash change too. This is bad pratice. - February 7, 2011 at 10:16 AM
- Rod said...
- I think I'm going to steal this as an interview question. Thank you for the idea :)
I took it upon myself to provide source for how to build your own (for educational purposes only) @ http://www.dzone.com/links/write_your_own_java_hashmap.html. I would expect a candidate to be able to pseudo-code something similar in an interview - or to admit they have no idea how it all actually works. - February 7, 2011 at 4:11 PM
- Javin @ Tibco RV Tutorial said...
- Hi Anonymous, Since HashMap is not synchronized there are issue while using it on multithreaded scenario e..g some times get() method of hashMap goes into infinite loop if rehashing and insertion occur in same time. there are other alternative available for multithreaded scenario e.g. ConcurrentHashMAp or Hashtable you can also see my article What is the difference between Synchronized Collection classes and Concurrent Collection Classes ? When to use what ?
- February 8, 2011 at 4:33 AM
- Pallavi said...
- This is really helpful.
I want to know how hashset is implemented in java. Do we need to override equals method in addition to hashcode? - February 13, 2011 at 8:56 AM
- 0x1e said...
- Hi Pallavi,
Internally HashSet is backed by HashMap. And generally yes, in case you have custom hashCode, you may want to override equals so they are consistent with one another.
Cheers
Oleksiy - March 8, 2011 at 5:43 AM
- Anonymous said...
- This is unrealistic, why did you focus on collisions? Java handles that problem as you pointed out, but quite frankly if you're worried about collisions maybe you should be more worried about your perception of hashing. If collisions would be the problem, then you're doing it wrong, you should not use an hashmap in the first place.
I'm sorry but discussing this sounds a bit stupid to me. If you would ask when an hashmap should be used and why, you'll be selecting better candidates. - April 30, 2011 at 4:13 AM
- Javin @ FIX Protocol Tutorials said...
- Hi Anonymous, I appreciate your thoughts but purpose here is to check whether interviewee is familiar with some common programming concepts or not. would you like to hire a guy who is familiar with how hashmap internally works or the one who just simply used hashmap for years but never bothered to think aabout it ? on a separate note yes your question "when an hashmap should be used and why" is also a starter on this topic.
Thanks for your comment. - May 3, 2011 at 6:02 AM
- Ramakrishna said...
- Put code snippets and add few points why you have used HashMap over other data structure.
Also mention some of the improvements that can be made to your HashMap code snippets. Improvement w.r.t. performance,accuracy of retrieval.
it would be nice to mention overriding equals() method? - May 5, 2011 at 9:04 AM
- Javin @ FIX Protocol and Electroinc Trading said...
- @Ramakrishna , those are actually discussed but yes as a part of actual interview , It does make sense to ask interviewee to write equals() and hashcode() implementation for any class along with those followup.
- May 14, 2011 at 8:23 PM
- Tanito said...
- Nice article.
- June 30, 2011 at 9:56 AM
- Anonymous said...
- Can you explain bit more about put() operation in hashmap and get() operation in hashmap. Also what happens if one thread is iterating over hashmap and other thread is put element into hashmap ? or one thread iterates over hashmap and other thread get() elements from hashmap , any code example will be good. anyway nice java hashmap interview question.
- July 7, 2011 at 6:33 PM
- Anonymous said...
- I was looking for some information on how hashmap works internally and found your site, you covered almost everything related to what is hashmap in Java, How HashMap works internally in java and more importantly how get method of hashmap works in java. kudos to you buddy. thanks a lot
- July 9, 2011 at 2:46 AM
- Javin said...
- Hi Anonymous 1 , put operation in hashmap is used to insert object into hashmap, put method of hashmap takes two argument one is key object and other is value object. always make sure your key object in hashmap is immutable. how get method of hashmap works is clearly explain in the article itself.
- July 9, 2011 at 8:48 PM
- SHASHIDHAR Y said...
- Hi,
Nice article. This was the exact sequence I was asked in an interview and I totally messed it up due to lack of knowledge about equals and hashcode methods. Hope, After reading this, I will never ever repeat the same mistake. - July 13, 2011 at 11:53 PM
- Roger said...
- Hi Javin, I am working on electronic trading system which we are going to design for foreign exchange and currency trading. I have some question related to FIX Protocol and how we can use FIX Protocol for currency trading. Can you please help me. since I don't have any prior experience on writing any electronic trading system, any suggestion will be appreciated
- July 17, 2011 at 1:56 AM
- Anonymous said...
- Nice Article. I understood how the insertion & retrieval work in HashMap. However, I have a very fundamental question:
As per java,
If two objects are equal by equals() method then their hashcode must be same.
Now, what is the rule is not followed.
Lets say we have 2 objects, obj1 & obj2. Lets assume equals method returns two on these 2 objects however the hashCode returns 2 different numbers.
If we try to add these 2 objects (as key) into a hashmap, we will end up having these 2 objects(& associated values) stored in 2 different buckets. (Had the hash code returned the same numbers, the second put() method would just have updated value of the first entry). So no issue with put() method.
Now when you try to retrieve values from hashmap using those two objects, you will successfully get 2 distinct entries created above. So, what's that I am missing?
Thanks. - July 24, 2011 at 12:32 AM
- Anonymous said...
- Hi, Is it possible to replace HashMap in Java with ConcurrentHashMap ? we have a project where hashmap has used a lot and now we want to replace that with ConcurrentHashMap.
- July 27, 2011 at 6:07 PM
- NaiveGeek said...
- Hi ,
Thanks for presenting this article very clearly. I have been working for more than 5 yrs in java until now but never got the proper reason behind how hashmap works.
thanks for such a information
Best
NaiveGeek - July 31, 2011 at 8:48 PM
- Anonymous said...
- @Anonymous you can't just replace HashMap with ConcurrentHashMap because one is not synchronized at all while other provides synchronization. you probably wanted to ask replacing Hashtable with ConcurrentHashMap but even on that case you need to make sure you are not entering into race condition by using putIfAbsent() method instead of put.
- August 4, 2011 at 6:51 PM
- Anonymous said...
- I think your May 3 comment reveals that your approach is unreasonable. You say, "...I appreciate your thoughts but purpose here is to check whether interviewee is familiar with some common programming concepts or not. would you like to hire a guy who is familiar with how hashmap internally works or the one who just simply used hashmap for years but never bothered to think aabout it ?..."
Do you understand everything you've used for years? How does an internal combustion engine work? How about your microwave? How are your clothes made? You've used your body for years, so you should understand anatomy and physiology? Getting closer to programming, how is a keystroke sent to your text editor? What happens if you hit two keys simultaneously? How is the file system implemented? How does the compiler transform Java code to byte code? In Java, do you understand type erasure, generics, wildcards? By your logic, you should since you use it all the time.
Would you agree that we could find an everyday topic that you're not an expert at? If so, I think your interview should be more reasonable. Joel Spolsky is known for hiring some of the industry's truly best people; you can see how he does it here: http://www.joelonsoftware.com/articles/fog0000000073.html - August 29, 2011 at 7:34 PM
- Javin @ enum in java said...
- @Anonymous,Thanks for your comment. I see you have a valid point but don't you agree that familiarity with common programming techniques and concept is essential especially if you are a professional programmer, A doctor definitely knows about anatomy of body , a mechanic definitely knows how internal combustion engine work. Also I have not said that don't hire a person because it just doesn't understand how hashmap works in java or what is difference between hashtable and hashmap, this question is more to see his appetite and attitude about its works and technology. Thanks for the link I been following Joel and benefited from his blog.
- August 29, 2011 at 8:29 PM
- Anonymous said...
- Awesome post. You've demonstrated your knowledge and desire to learn--you're one of the good guys, no doubt.
"... this question is more to see his appetite and attitude about its works and technology." Awesome! I agree is a fantastic thing to test an interviewee on, but I wonder if one could ask it more directly? Perhaps something like, "Can you tell me about the last time you really dug deep to understand a programming concept?" Maybe more abstractly as in "What are you passionate about?" or "What do you read?" - September 6, 2011 at 11:08 AM
- Javin @ String and Stringbuffer in java said...
- Thanks Anonymous for your kind comment. Yes those direct questions can be another good alternative.looking forward to see you again.
- September 7, 2011 at 7:03 AM
- Anonymous said...
- Amazing article. I wish i could have found this blog before my interview, where i was asked the very questions that you have answered. I was asked to write a HashMap implementation of my own and after reading this article it looks like i could have given a decent try.
- September 29, 2011 at 1:23 AM
- Gokul said...
- Nice article Javin! Would be nice if you can talk more about how this discussion leads onto ConcurrentHashMap...
- October 9, 2011 at 2:04 PM
- Javin @ OutOfMemoryError in Java said...
- You have a point Gokul , ConcurrentHashMap is very popular data-structure for high frequency trading platform and it has lots of details which can be tested like Can we replace hashtable with ConcurrentHashMap, may be I will write on that sometime. Thanks for your comments.
- October 10, 2011 at 5:27 AM
- Javin @ Generics in Java said...
- Thanks for your comment Swapna, Sample priciple applies on hashtable as well. you may also like my article How substring method works in Java
- October 14, 2011 at 6:14 PM
- Javin @ Date To String in Java said...
- @Viraj, @ Anonymous and @Bhaskara , Thanks for your comments guys and good to know that you like my hashmap interview experience. you may also like How Substring works in Java
- October 29, 2011 at 7:13 AM
- Anonymous said...
- This tutorial is simple and clear, very good.
May I ask a question that is about equal and hashcode?
I can't understand why the following code yield "False" ?
if( new Boolean("True") == new Boolean("True"))
System.out.println("True");
else
System.out.println("False");
I conduct 2 tests and still don't understand why the above is "False".
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("True");
b1 and b2 is the same by the equal method
System.out.println(b1.equals(b2));
Hash code are the same
System.out.println(b1.hashCode());
System.out.println(b2.hashCode());
Thanks - November 15, 2011 at 8:30 AM
- Rosario said...
- because the operator "==" is used to verify the matching of objects reference, not the value. In other word you have created two distinct object using "new Boolean);" so you have different reference for two object allocated in different area of memory. I f you want compare object of same class and you want know if they have same hashcode you should use the method Equals(, it works well in this case in other case for you need to use the method compareTo() and compare().
Bye! - November 20, 2011 at 8:31 AM
- Surya said...
- Hi ,
Anonymous brought up a valid point. Both equals() and hashCode() on an object should confirm the object equality. "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result." . - November 20, 2011 at 5:46 PM
- Anonymous said...
- Hi,
Sorry but I think that asking these kind of questions in a job interview proves nothing. Today you can find anything you need on the web so there's no point in memorizing implementations of HashMap, HashTable etc.
I believe that your questions could be good if you would ask about hashing in general (two objects has the same hashcode etc).
The most important thing for a developer, TMHO, is that she could THINK, not memorize...
Kind regards
N - November 27, 2011 at 8:08 PM
- Anonymous said...
- Isn't other collection classes which is based on hashing e.g. HashSet or LinkedHashMap also works in Same principle ? Does HashSet also use equals and hashcode to insert and retrieve objects ?
- December 19, 2011 at 7:42 PM
- Anonymous said...
- does it stores key and value object in different node in the linked list or in same node?
- January 1, 2012 at 10:54 PM
- Javin @ jsp interview questions answers said...
- Hi Anonymous, keys and values of One Entry in HashMap is stored at one node and that's how after finding bucket location it finds correct object in hashmap.
- January 2, 2012 at 12:03 AM
- shivu said...
- Good article, but created some confusion in my mind...
If the computed hashcode is same for two objects, hashMap will override the existing value for that key.
My question is what is the scenario the value would be overriden and added to same bucket in the HashMap.
The piece of code placed here for overriding
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println("s1 hashcode: "+s1.hashCode());
System.out.println("s2 hashcode: "+s2.hashCode());
Map map = new HashMap();
map.put(s1, s1);
map.put(s2, new String("cdf"));
System.out.println(map); - January 4, 2012 at 3:08 AM
- Javin @ spring interview questions answers said...
- Hi Shivu, if key is same i.e. equals by equals method in Java than hashcode would be same and value will be replaced but if key is not same i.e. not equal but hashcode is same (Which is possible in java) than bucked location would be same and collision would happen and second object will be stored on second node of linked list structure on Map.
- January 4, 2012 at 4:59 AM
- Gaurav said...
- isn't it all other hashing related data-structure like Hashtable, HashSet, LinkedHashMap is implemented in this way ? At least implementation of put() and get() should be same with some addition specific details related to individual collection.
- January 5, 2012 at 4:59 PM
- Mandakini Mishra said...
- You are just awesome.Have no words to praise you.You have explained in such difficult thing in such a simple way.Fantastic, Great work.
Mandakini - January 17, 2012 at 2:49 AM
- Anonymous said...
- hi,
still i have little confusion over this
can you specify an example how can two different objects has same hash code(say for two custom objects) - January 28, 2012 at 7:32 PM
- MJB said...
- In fact Hashset in java is simply a delegated hashmap: here's a snippet of the source to prove the point
private transient HashMap map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
// similar methods that delegate follow - February 1, 2012 at 7:53 PM
- Joe said...
- Thanks for such nice article. Will be expecting such articles in future also.
- February 1, 2012 at 9:45 PM
- Javin @ HashMap vs HashSet said...
- @MJB, Indeed HashSet uses HashMap for hashing support and thank for putting the code snippet it makes the point much clearer.
- February 3, 2012 at 10:32 PM
- Javin @ hashtable example java said...
- @Mandakini Mishra , Thanks for your kind words, glad to hear you like this Java hashmap question answer article.
- February 3, 2012 at 10:33 PM
- Anonymous said...
- This is a great article, but if i have one complaint, it seems to be written poorly in terms of english (almost as thought a translator was used). i am finding it hard to understand some of the answers because of this.
- February 12, 2012 at 4:24 PM
- Anonymous said...
- I have one comment regarding your explanation: key.equals() is used also when you use put() method, because if hashcode() of an existing key is the same with the one which you want to insert, yes, the new key will be the next one in the bucket, ONLY IF THE KEY IS UNIQUE. So in order to do that, it is used also method key.equals(), to prevent duplicates. (You said that we are talking about equal() method only when we are retriving data from HashMap)
Regards - February 19, 2012 at 9:58 AM
- Javin @ collection interview question said...
- @Anonymous, your observation is correct key.equals() is used on both put() and get() method call if object already exits in bucked location on hashmap and that's why tuples contains both key and value in each node. thanks for pointing it and making it more clear.
- February 20, 2012 at 5:35 AM
- Anonymous said...
- Hi,
Overriding equals and hashcode we can make the object as key in hashmap.While putting the the reference to the object as Key in hashmap or creating the object and setting it in put method , what difference does it make in retreiving the object? - February 21, 2012 at 11:02 AM
- Javin @ producer consumer problem java said...
- @Tofeeq thanks for your comment. Glad to hear that you like interview question on java hashmap.
- February 24, 2012 at 7:23 AM
- P-H said...
- Thanks Javin, great post again.
This is true that Hashmap are common questions someone can get in interview from a Java & Java EE perspective. I interview new members myself at my current company (CGI Inc.) and the typical questions that I ask, in a Java EE context are:
- What are best & known risks when using Hashmap data structure?
- What JDK Map data structure is more suitable to handle concurrent read & write operations in a Java EE environment?
The answer for the first one is all about adding proper synchronization when Hashmap is used in a concurrent Thread access scenario e.g. same Hashmap instance shared across multiple Threads.
Primary known risks are Hashmap internal index corruption and infinite looping, which can bring your JVM to its knees.
The answer about the second one is to use the ConcurrentHashMap, introduced in JDK 1.5, which provides Thread safe operations but no blocking get() operation; which is key for proper performance. This is the typical data structure used these days in modern Java EE container implementations.
If your readers are interested, I have a case study of a recent Hashmap infinite looping problem, actual Weblogic 11g defect (non Thread safe Hashmap)!
Thanks again for all that good work.
P-H - February 26, 2012 at 6:29 AM
- ADNAN ALI said...
- I am new to Java World! So I have this problem stated as under.....
I have sortedHashMap and hashMap2
Now i have to get hashMap3 based on sortedHashMap sorted order.
Secondly; I have also to limit hashMap3 as of first sorted 100 values.....rest map I dont need.
Please suggest me coding example or junk of code.
Thanks! - February 28, 2012 at 1:44 AM
- Neeru said...
- I was always curious on How HashMap stores object in Java or How HashMap is implemented in Java. I know about bucket and collision but What I missed was Map.Entry which is used to store both key and value object. HashMap stores Entry object which contains both key and value which then used in case of collision when more than one object is stored inside bucket to find out correct mapping. Correct me if this is not how hashmap stores objects in Java.
- February 29, 2012 at 8:20 PM
- Ski said...
- Isn't it Hashtable also works in similar way. Please let us know How Hashtable works in Java. I know about HashMap but now I am keen to know How Hashtable works internally.
- February 29, 2012 at 8:24 PM
- Anonymous said...
- Hi Javin,
Nice artcile..nicely explained..it wud have been great it u cud explain the rehashing of Hashmap and the concurrent thread access in Hashmap. - March 4, 2012 at 5:02 PM
- Anonymous said...
- Excellent article....
But I would correct on one point.
>What will happen if two different HashMap key objects have same hashcode?
>They will be stored in same bucket but no next node of linked list. And keys equals () method will be used to identify correct key value pair in HashMap.
The next node of the linked list will point to the next object in the same bucket. - March 6, 2012 at 2:08 AM
- Anonymous said...
- I liked a post a lot .
I am undertaking interviews and i know that these questions are really difficult to ans , inspite of working on hash map , we generally forget these details .
Thanks - April 3, 2012 at 2:38 AM
- Ratnakar Reddy Katipally said...
- Thanks a lot. Pretty clear :) :)
- April 9, 2012 at 5:42 AM
- Sriram said...
- Thanks for the post.Its really useful.
1)Can you be more clear on using the final immutable objects as keys?
2) what are the advantages of having final immutable objects as keys?
3)Does they have any performance impact while getting the hashcode or while performing the equals method?
I feel Collision should actually happen to improve the performance.Only then there will be a performance gain while searching for an object.If 1000 objects have 1000 different hashcodes, we will end up with 1000 hash buckets and each containing only one element.In that case, there wont be any use of hashmap and it's same as Arraylist.
4)So, How do we make sure that multiple objects have the same hash code?
5) Hash map should have unique keys right.So if we try to insert an object with the key that was already present in the HashMap, why does it not throw any exception.Instead it will replace that value object in the HashMap.Is there any reason for this?
6) Why did they implement this key value pair? I mean Why cant it be just the value object thereby making the HashMap calculating the hascode of value and use the same while retrieving?If we dont want a key value pair and just want to improve search performance, then is it not this key value pair adding overhead to performance? If I want the same fucntionality as hashmap but dont need the key value pair, which collection do you suggest?
Sorry for more questions on this.. I just thought to be very much clear on this HashMap concept.
Please let me know if am not clear.
Read more: http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html#ixzz3gj3squhQ
Subscribe to:
Posts (Atom)

.jpg)
14 comments :
interviewer was possibly expecting a simple answer, such as:
•if the whole map is synchronized for get/put operations, adding threads won't improve throughput because the bottleneck will be the synchronized blocks. You can then write a piece of code with a synchronizedMap that shows that adding threads does not help
•because the map uses several locks, and assuming you have more than one core on your machine, adding threads will improve throughput
The example below outputs the following:
Synchronized one thread: 30
Synchronized multiple threads: 96
Concurrent one thread: 219
Concurrent multiple threads: 142
So you can see that the synchronized version is more than 3 times slower under high contention (16 threads) whereas the concurrent version is almost twice as fast with multiple threads as with a single thread.
It is also interesting to note that the ConcurrentMap has a non-negligible overhead in a single threaded situation.
This is a very contrived example, with all the possible problems due to micro-benchmarking (first results should be discarded anyway). But it gives a hint at what happens.
public class Test1 {
static final int SIZE = 1000000;
static final int THREADS = 16;
static final ExecutorService executor = Executors.newFixedThreadPool(THREADS);
public static void main(String[] args) throws Exception{
for (int i = 0; i < 10; i++) {
System.out.println("Concurrent one thread");
addSingleThread(new ConcurrentHashMap ());
System.out.println("Concurrent multiple threads");
addMultipleThreads(new ConcurrentHashMap ());
System.out.println("Synchronized one thread");
addSingleThread(Collections.synchronizedMap(new HashMap ()));
System.out.println("Synchronized multiple threads");
addMultipleThreads(Collections.synchronizedMap(new HashMap ()));
}
executor.shutdown();
}
private static void addSingleThread(Map map) {
long start = System.nanoTime();
for (int i = 0; i < SIZE; i++) {
map.put(i, i);
}
System.out.println(map.size()); //use the result
long end = System.nanoTime();
System.out.println("time with single thread: " + (end - start) / 1000000);
}
private static void addMultipleThreads(final Map map) throws Exception {
List runnables = new ArrayList<> ();
for (int i = 0; i < THREADS; i++) {
final int start = i;
runnables.add(new Runnable() {
@Override
public void run() {
//Trying to have one runnable by bucket
for (int j = start; j < SIZE; j += THREADS) {
map.put(j, j);
}
}
});
}
List futures = new ArrayList<> ();
long start = System.nanoTime();
for (Runnable r : runnables) {
futures.add(executor.submit(r));
}
for (Future f : futures) {
f.get();
}
System.out.println(map.size()); //use the result
long end = System.nanoTime();
System.out.println("time with multiple threads: " + (end - start) / 1000000);
}
}
Then how the putIfAbsent will work, the example you gave..if(map.get(key)==null)
That's imprecise and misleading.
From the Javadoc:
"Retrievals reflect the results of the most recently completed update operations" (for put() and remove()).
"For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries"
if updates are not synchronized then how come it has thread safe writes?