• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashSet internal working

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet:This class implements the Set interface, backed by a hash table (actually a HashMap instance).

While adding elements into HashSet i never specify the key and offcourse it does not make sense to specify the key since its a set. I am wondering since the HashSet is implemented via a HashMap instance , what would be the key that would be used to put data into HashSet.

The answer is they key is a the value that is passed into the the hashset. For example
HashSet<String> names = new HashSet<String>();
names.add("Deepak");

Internally since the HashSet is implemented by a HashMap instance , and HashMap requires a key and value. So here the key = "Deepak" and value = "new Object()". The Value is a dummy object.Which is infact a static constant so that each add operation does not create a dummy instances of Object.
Hence names.add("Deepak");
gets converted to
internalHashMap.put("Deepak",object);
Here object defined as
private static final Object object = new Object();

I had this doubt but after reading a bit i got the solution as well. Hence i thought i would post it.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet uses HashMap internally to store objects.
you are saying

HashSet<String> names = new HashSet<String>();
names.add("Deepak");

when you add object("Deepak") internall it is stored in HashMap where
key is "Deepak" & value is dummy object
But when you iterate through Hashset means internally you are iterating thtough HashMap.In HashMap key is "Deepak" & value is dummy object, so it should return dummy object rather than key i.e. "Deepak" . So you are getting dummy object out of the HashSet not orignal object ("Deepak").

I am not getting your point ,Will you explain please?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all Deepak thanks for sharing the info. I used to think that the hasMap contains the hash values as the keys and the actual object that we put in the hasSet as the values to the keys.

Siraj when you iterate over the internal HashMap, the iteration will be over the keySet of the hashMap. As you know that you can call keySet on a map to get the keys as a set of values. So this must be the internal implementation although I am not sure...
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question.
As you know to get the data from the HashSet you would need to use the iterator.
HashSet<String> hashSet = new HashSet<String>();
hashSet.add("Test");
hashSet.add("Test1");
hashSet.add("Test2");

Hence when you use iterator as follows
Iterator<String> iter = hashSet.iterator();
Here hashSet.iteator() would return you the iterator of the Keys and not the values as shown below
internalHashMap.keySet().iterator();

And hence when you iterate over an HashSet you would see the data that you entered which are infact the keys into HashMap and not the dummy object.

Hope this clears
 
siraj siddiqui
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak & Ankit thanks for the information. But i still have a doubt.
Will you explain,
Why HashMap do not use the hashcode value as a key and actual object as a value.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its just a matter of implementation
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Information :

Here is the internal HashMap

private transient HashMap<E,Object> map;


and here is the Dummy Object that is static final.

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

this is the no-arg constructor
public HashSet() {
map = new HashMap<E,Object>();
}
HashMap default capacity is 16.



public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}


public void clear() {
map.clear();
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the best explanation of how hash set works in java

http://javahungry.blogspot.com/2013/08/how-sets-are-implemented-internally-in.html

I think it almost resembles the same as mentioned above in the forum , but the examples explained in the shared link made it much easy for the reader to understand .
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic