tag:blogger.com,1999:blog-8712770457197348465.post6048317060315693232..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: Difference between ConcurrentHashMap, Hashtable and Synchronized Map in Javajavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger48125tag:blogger.com,1999:blog-8712770457197348465.post-23497329104045447642021-09-17T22:14:21.845-07:002021-09-17T22:14:21.845-07:00Hi, I got the difference between concurrentHashMap...Hi, I got the difference between concurrentHashMap and synchronized map. But Synchronized map is throwing concurrentModificationException as per example on https://www.javainuse.com/java/javaConcurrentHashMap<br />Then what is difference Between Normal HashMap and SynchronizedMap ??ayushOnJavaHibernate1https://www.blogger.com/profile/01050869099911237534noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57711396284387472132021-09-08T21:34:17.403-07:002021-09-08T21:34:17.403-07:00while segment is locked for write, whether read is...while segment is locked for write, whether read is allowed on that segment?Srinihttps://www.blogger.com/profile/02682982365595529596noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69589089006587289292018-01-17T20:18:17.209-08:002018-01-17T20:18:17.209-08:00Yes. It locks the entire map.Yes. It locks the entire map.Santhoshhttps://www.blogger.com/profile/04800617779072159036noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67208573269378439522017-01-17T12:55:37.302-08:002017-01-17T12:55:37.302-08:00Very good explanation Very good explanation Indrani Senhttps://www.blogger.com/profile/04164977432512342430noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61932789155479324142016-12-23T13:56:46.887-08:002016-12-23T13:56:46.887-08:00Thanks for writing all these articles. Very helpin...Thanks for writing all these articles. Very helping.Darpanhttps://www.blogger.com/profile/01886409854030467940noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41149656002717486162016-12-21T01:40:13.769-08:002016-12-21T01:40:13.769-08:00Nice article!Nice article!Osmand Prashant Dashttps://www.blogger.com/profile/04092012013241730717noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30363512908537269762016-10-08T12:04:09.254-07:002016-10-08T12:04:09.254-07:00Very neatly explained, very useful!Very neatly explained, very useful!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65059197947761656402016-08-14T03:41:46.898-07:002016-08-14T03:41:46.898-07:00Hi,
I have a question..
As you said, CHM is usin...Hi,<br /><br />I have a question.. <br />As you said, CHM is using stripped locking technique, there it locks certain segment while doing any change, So it means multiple threads not only read can also modify CHM concurrently. Anonymoushttps://www.blogger.com/profile/02775354816082002552noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90044834313681721172016-07-06T13:10:11.397-07:002016-07-06T13:10:11.397-07:00Hi,
I have one question, how do the ConcurrentHas...Hi,<br /><br />I have one question, how do the ConcurrentHashMap handles resizing?<br />If more than 16 thread work on same ConcurrentHashMap , what will handle and how?Anonymoushttps://www.blogger.com/profile/08693356601374617586noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-89578953720316862832016-03-14T01:43:01.143-07:002016-03-14T01:43:01.143-07:001st of all, hashTable and hashMap are different in...1st of all, hashTable and hashMap are different in nature of storing keys and data. Hashmap allows one null key and many null values, while hashTable doesn't allow any null key or value. So collections.synchronizedMap(new HashMap()) makes the hashmap only thread safe, rest of its feature remains same. In this respect, collections.synchronizedMap(new HashMap() and hashTable are not redundant juliahttps://www.blogger.com/profile/07241088216714241329noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45191149225702768192016-01-25T18:46:11.463-08:002016-01-25T18:46:11.463-08:00Use synchronized blockUse synchronized blockAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-46575933664973939892016-01-23T00:44:54.340-08:002016-01-23T00:44:54.340-08:00@sharyu, read Java Generics and Collection, it'...@sharyu, read Java Generics and Collection, it's one of the top book in my list of <a href="http://javarevisited.blogspot.com/2013/01/top-5-java-programming-books-best-good.html" rel="nofollow">must read Java books</a>javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12266961167132746452016-01-23T00:42:31.281-08:002016-01-23T00:42:31.281-08:00Hi, could you suggest a book which will give the i...Hi, could you suggest a book which will give the in detail information along with comparison of all the collections in java?sharyuhttps://www.blogger.com/profile/10647324929857298018noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13733894904993650212015-12-21T02:16:58.526-08:002015-12-21T02:16:58.526-08:00Hi,i was asked in an interview as default intial c...Hi,i was asked in an interview as default intial capacity of concurrent hash map is 16,so 16 threads can operate in parallel but what happens if there are more than 16 threads for write operation? <br /> Anonymoushttps://www.blogger.com/profile/16969090238267955566noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54577651978979521572015-09-04T20:11:41.203-07:002015-09-04T20:11:41.203-07:00@Anonymous, 'single collection wide lock' ...@Anonymous, 'single collection wide lock' means whole collection will be locked when one thread does read, write or update. This means even if two threads are reading values, they will feel contention. This is optimized in ConcurrentHashMap and CopyOnWriteArrayList by using sophisticated technique of portion wide lock and copying the collection when change to give better performance and javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17377722385602562612015-09-01T02:36:13.992-07:002015-09-01T02:36:13.992-07:00Hi. What do you mean by 'single collection-wid...Hi. What do you mean by 'single collection-wide lock' in the part 'Why need ConcurrentHashMap and CopyOnWriteArrayList' of the article? <br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-91922283950989871812015-07-11T04:36:21.076-07:002015-07-11T04:36:21.076-07:00@Anonymous (November 25th), null keys are handled ...@Anonymous (November 25th), null keys are handled specifically in HashMap, they are stored at the first index in bucket array e.g. at index 0. So anytime, if you wan to retrieve value associated with null keys in HashMap it simply return table[0], it never call hashcode() method there to find the bucket location. <br /><br />Regarding your second question, what will happen if you cast HashMap javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14018333295517868502015-06-28T16:15:05.783-07:002015-06-28T16:15:05.783-07:00Good article. Thanks.
In hashmap it will overwrite...Good article. Thanks.<br />In hashmap it will overwrite the values for NULL key. For ex: map.put(null, "abc"); map.put(null, "lmn");<br /><br />Then last value will be there in hashmap with only one null key though you inserted N number of keys as null.<br /><br />to get the null key value: map.get(null) will print "lmn" here.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21277406137120865692015-05-14T12:30:20.225-07:002015-05-14T12:30:20.225-07:00how is partition done and how lock is done ? how is partition done and how lock is done ? Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1629579171475057222014-12-21T05:29:54.725-08:002014-12-21T05:29:54.725-08:00Hi ,
You can't do like this HashTable m1 = (Ha...Hi ,<br />You can't do like this HashTable m1 = (HashTable)m; .<br /><br />If you want to put map into Hashtable you need call like this m1.putAll(m);<br />But If you do this you will get nullpointer exception like below. So Java taken care to handel null while converting Hashtable from map.<br /><br />Exception in thread "main" java.lang.NullPointerException<br /> at Gopalhttps://www.blogger.com/profile/07787775452697831077noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82816948855681865322014-11-25T22:26:07.961-08:002014-11-25T22:26:07.961-08:00I was asked this question,
HashMap allows one nul...I was asked this question,<br /><br />HashMap allows one null key and multiple null value.<br /><br />So if I do:<br /><br />Map m = new HashMap(null,"abc");<br /><br />then how will I be able to retrieve this value "abc"?<br /><br />Also in future if I do:<br /><br />HashTable m1 = (HashTable)m;<br /><br />since HashTable does not all null keys, what will happen in this case?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87962378059539175732014-09-16T07:37:01.710-07:002014-09-16T07:37:01.710-07:00@Atmprakash, it depends upon usage scenario. If yo...@Atmprakash, it depends upon usage scenario. If you are running with just one two thread then not much difference, but if you are running with say 20 threads, ConcurrentHashMap will perform much better than Hashtable and Synchronized Map, because CHM uses lock stripping and multiple thread can read from Map simultaneously. See my post on <a href="http://javarevisited.blogspot.sg/2013/02/javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7077486396691212072014-09-16T04:59:20.806-07:002014-09-16T04:59:20.806-07:00i have question ???
whose performance will be bett...i have question ???<br />whose performance will be better ???Anonymoushttps://www.blogger.com/profile/05805400109920741751noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6015476239738506142014-07-27T13:39:25.384-07:002014-07-27T13:39:25.384-07:00Raj,
Both(HashTable & Collections.Synschroniz...Raj,<br /><br />Both(HashTable & Collections.SynschronizedMap()) are synchronized version of collection. Both have synchronized methods inside class. Both are blocking in nature i.e. multiple threads will need to wait for getting the lock on instance before putting/getting anything out of it.<br /><br />So what is the difference. Well, NO major difference for above said reasons. Performance Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77046104593127273832014-07-09T08:54:47.396-07:002014-07-09T08:54:47.396-07:00Hi I got below question in interview, can some one...Hi I got below question in interview, can some one help on this?<br /><br />what is the difference between HashTable and collections.synchronizedMap(new HashMap()).I mean when we already have Hashtable in place to be used in concurrent environment, is not it collections.synchronizedMap(new HashMap())is the redundant feature?Rajnoreply@blogger.com