HashSet :
HashSet<String> hset=new HashSet<String>();
It is implementation of Set interface. It stores unique elements, even if we pass duplicates, it will override the elements. So ever you wondered how this uniqueness is manitaned in HashSet. Let us understand.
HashSet internally uses HashMap. The element we add in set, is stored as a key in hashmap and some dummy value is inserted corresponding to that key. as we know that always key is unique in hashmap, even if we pass duplicate key, it will just override it. So this is how uniqueness in HashSet is maintained.
In similar ways, TreeSet internally uses TreeMap, LinkedHashSet internally uses LinkedHashMap.
HashSet<String> hset=new HashSet<String>();
It is implementation of Set interface. It stores unique elements, even if we pass duplicates, it will override the elements. So ever you wondered how this uniqueness is manitaned in HashSet. Let us understand.
HashSet internally uses HashMap. The element we add in set, is stored as a key in hashmap and some dummy value is inserted corresponding to that key. as we know that always key is unique in hashmap, even if we pass duplicate key, it will just override it. So this is how uniqueness in HashSet is maintained.
In similar ways, TreeSet internally uses TreeMap, LinkedHashSet internally uses LinkedHashMap.
No comments:
Post a Comment