filmov
tv
HashSet in Java- Interview Cracker

Показать описание
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.
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.
Комментарии