tag:blogger.com,1999:blog-694855878384792308.post5648990442181417924..comments2024-03-21T06:26:49.387-07:00Comments on Java67: How HashSet works in Java [Explained with Example]javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger21125tag:blogger.com,1999:blog-694855878384792308.post-56028532545827127992020-03-05T04:31:10.439-08:002020-03-05T04:31:10.439-08:00internals are covered brilliantly
internals are covered brilliantly <br />Anonymoushttps://www.blogger.com/profile/16918461961206310959noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-30859259420402934482017-12-25T10:25:51.047-08:002017-12-25T10:25:51.047-08:00Great Concepts Great Concepts Ashish DeoVanshihttps://www.blogger.com/profile/17611431502875599281noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-32547317804138452932017-02-17T12:46:36.391-08:002017-02-17T12:46:36.391-08:00Hi, Thank you for such wonderful explanation.
1. P...Hi, Thank you for such wonderful explanation.<br />1. Please clear my doubt, when you say "Immutability is not requirement but if its immutable then you can assume that object will not be changed during its stay on set". When can the objects change in absence of immutability.<br />2. <br /><br />2. How hashset reacts if new object's hashcode is same as of existing object. So, probably as the underlying structure is map, so, a linked list will be formed but what will happen if we try to iterate that hashset. <br /><br /><br />ANKUSH RAINAhttps://www.blogger.com/profile/01758862604310723138noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-33078070337339073732016-08-11T01:38:59.554-07:002016-08-11T01:38:59.554-07:00great info thanks.great info thanks.urbanhttps://www.blogger.com/profile/01060116052157335950noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-26928499286968935962015-09-16T03:14:31.970-07:002015-09-16T03:14:31.970-07:00Yes, HashSet allows only one null value.Yes, HashSet allows only one null value.Foramhttps://www.blogger.com/profile/14374573491907966517noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-81037398269592383722015-08-19T04:31:46.371-07:002015-08-19T04:31:46.371-07:00Hi,
Can anyone explain "if set intenally impl...Hi,<br />Can anyone explain "if set intenally implements HashMap then HashSet should allow atleast 1 null"..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-37104950369591497112015-02-01T13:50:55.892-08:002015-02-01T13:50:55.892-08:00I was wondering why does not the Set range also ha...I was wondering why does not the Set range also have something like 'SetMapHash', so that you can have index-based access (for fast retrieval) and also ensuring the uniqueness of stored objects?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-9351643592855013192014-12-20T12:48:55.729-08:002014-12-20T12:48:55.729-08:00Hello,
if load factor is 0.75 and initial capacit...Hello,<br /><br />if load factor is 0.75 and initial capacity is 16.<br />After inserting 12 elements what will be the new Capacity?<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-7641227739982112302014-12-01T03:16:26.652-08:002014-12-01T03:16:26.652-08:00Hii, my question is what is table in hashmapHii, my question is what is table in hashmapAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-58136884701505633882014-10-05T01:51:08.214-07:002014-10-05T01:51:08.214-07:00Yes, that's true because it internally uses Ha...Yes, that's true because it internally uses HashMap, so whatever implementation of HashMap is available it also applicable to HashSet. For Example, from Java 8 onwards, HashMap will switch to tree data structure instead of linked list if collision crosses a certain threshold to keep HashMap performance healthy. Because in case of heavy collision your Map will turn into a linked list and a get() will take O(n) instead of intended O(1).javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-36322866755722161702014-10-05T00:09:38.137-07:002014-10-05T00:09:38.137-07:00in case of hashmap we will have a linked list in e...in case of hashmap we will have a linked list in each bucket and will store the key's having same hash code in a linked list. Is it the same here too?Anonymoushttps://www.blogger.com/profile/07622376188273692276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-9998329370109579922014-09-17T05:40:21.308-07:002014-09-17T05:40:21.308-07:00Well Prateek, this is just one way to implement Ha...Well Prateek, this is just one way to implement HashSet. The advantage of this approach is that they don't have to implement HashSet from scratch, they can reuse HashMap code to the heavy lifting. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-48191897658385945172014-09-17T05:39:23.191-07:002014-09-17T05:39:23.191-07:00That's correct Amitesh, No null values are sto...That's correct Amitesh, No null values are stored, only PRESENT is stored, and that too is just one object, it's just references which are stored. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-24882922449214686282014-09-17T05:38:21.842-07:002014-09-17T05:38:21.842-07:00That's not true, PRESENT object is stored as v...That's not true, PRESENT object is stored as values for all keys. Look at the below code from add() method of HashSet :<br />public boolean add(E e) {<br /> return map.put(e, PRESENT)==null;<br />}<br /><br />You should not have doubt after looking at this code :)javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-90955342013866810972014-09-17T05:35:53.745-07:002014-09-17T05:35:53.745-07:00Thank You Jaya. If you like article, don't for...Thank You Jaya. If you like article, don't forget to share with your friends :-)javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-70602663922147122812014-09-16T11:31:17.557-07:002014-09-16T11:31:17.557-07:00Very helpful article. I am becoming a very BIG fan...Very helpful article. I am becoming a very BIG fan of your blog. The content you write is extremely simple to understand. Thanks!Jayahttps://www.blogger.com/profile/16203422141218956109noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-39801661410577003992014-08-29T06:15:27.785-07:002014-08-29T06:15:27.785-07:00But why HashSet cant have its own implementation ,...But why HashSet cant have its own implementation , why does it use HashMap, why do we have to store "PRESENT" constant unnecerrilyPrateek Rathorehttps://www.blogger.com/profile/04442332907033511811noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-28805777440314739782014-04-16T10:35:06.380-07:002014-04-16T10:35:06.380-07:00@Hi, He is right. Object we are adding to the set ...@Hi, He is right. Object we are adding to the set is internally added as a Key to the HashMap and value for the HahsMap in the internal implementation of HashSet will be same "PRESENT". Hence we are only responsible for our object value that we are adding to the set.<br />Thanks<br />Kumar Shoravshoravhttps://www.blogger.com/profile/12714211986672193807noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-25043990660673292412014-03-21T16:28:02.758-07:002014-03-21T16:28:02.758-07:00why would anyone like to store null values ?.
And...why would anyone like to store null values ?.<br /> And in fact values are never stored ,only the references pointing to those values(ie objects) are stored in any type of collection. Amitesh Rainoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-34462361931330267342014-03-11T06:20:43.540-07:002014-03-11T06:20:43.540-07:00As per my understanding I guess values are stored ...As per my understanding I guess values are stored as null for all keys.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-91468765403037542862014-01-24T01:35:10.385-08:002014-01-24T01:35:10.385-08:00Little confusion.
At the starting you said element...Little confusion.<br />At the starting you said elements are stored as keys with same value always but later you said the value for all keys is constant which is PRESENT.<br />Which of these is correct?Anonymousnoreply@blogger.com