HashSet in Java- Interview Cracker

preview_player
Показать описание
How HashSet works internally in Java? - Interview Cracker

Today's topic is How hash set works internally in java?
Let us first understand that:
Hashset allows no duplicates.
Hashset allows only 1 null value.
Hash set works on the principle of hashing.
Hash set only stores elements and NOT key value pair like hash map.
Hash set internally uses hash map for storing elements.
Let us see how elements are added in a hash set?
In hashset, from the add method, the put method of backing hash map is called, where the value, which has to be added in the hash set, becomes key and a constant object called PRESENT is used as value.
Present?? What is it?
Let us see.
In Hash set implementation PRESENT is defined as :
private static final Object PRESENT = new Object();
This is the dummy value to associate with an Object in the backing hash Map.

So we can notice that add method of hashes calls put method of backing hash map object.
It passes the element as key and a constant present is stored as it's value.
We know that in hash map value may be duplicate but keys should be unique.
That is why hash set has unique values.
Let us see how object is removed in hash set?
Internally, remove method of HashSet calls remove(Object key) method of the HashMap.

Here note that remove(Object key) method of the HashMap returns the Value associated with the key
But note that the remove(Object o) method of the HashSet returns Boolean value
We know that for every value added in HashSet that value becomes Key and the value is always an object called PRESENT
Now let us see how object is retrieved?
In HashSet there is no get method.
Here iterator is used to iterate through the values of the hash Set.
Internally it will call the keyset of the HashMap.
As values are stored as keys in the HashMap so we'll get the values stored in the HashSet.

The default initial capacity is 16
The default load factor is 0.75
Also do not forget to refer to our previous tutorial videos for understanding load factor and initial capacity.
Remember that internal working for hash set is an important interview question.
Рекомендации по теме
Комментарии
Автор

Good content 👍 This covers pretty much all thats asked during the interview.

vrushalipatil
Автор

But isn't the iterator of hashset a cyclic method. Since it calls the iterator of map.keyset.interator (keyset is a set) or is the keyset a different set subtype?

redcrafterlppa
Автор

but how internally how set know unique value

gauravsoni
Автор

Can concentrate well if there's no music. Remove it atleast from future videos.

pspkfan
Автор

Please dont add any background music. its very annoying while listening to the content

ManishPushkarVideos
Автор

Nice tutorial.Also please remove bg music, it is a noise.

half_demon
Автор

Useless music is very annoying while reading

salonisinghariya
Автор

please remove bg music. Content is understandable

ManojKumar-qehy
Автор

Nice Content.. Easy to Understand. Keep it up !!

swatisdelicacies
Автор

good explonation bad everything else music lame draws poor hand animarion

michaelstevefox
Автор

dont know why there are thumbs down on this video... this pretty much explains what we need for interview... tx

NitinSharma-sweh