Set works internally in Java.Internally, a HashSet stores its elements using a HashMap. A HashMap stores data as key-value pairs, whereas a Set contains only unique elements. When an element is added to a HashSet, it is stored as the key in the underlying HashMap, and a constant dummy object is used as the value for every entry.
Since HashMap does not allow duplicate keys, HashSet also does not allow duplicate elements. If you attempt to add a duplicate element, the add() method returns false, and the element is not added to the Set.
When a new element is added successfully, the add() method returns true. If the element already exists, it returns false. Therefore, adding a duplicate element does not result in a compile-time or runtime error—it is simply ignored.
nice post
ReplyDeletehashset uses hashmap to insert elements not hashtable
ok..thanks
ReplyDelete