![]() |
VOOZH | about |
The HashSetadd() method in Java is used to add a specific element to the set. This method will add the element only if the specified element is not present in the HashSet. If the element already exists, the method will not add it again. This method ensures that no duplicate elements are added to the set.
Example 1: This example demonstrates how to add elements to a HashSet.
HashSet: [1, 2, 3]
boolean add(E e)
Example 2: This example demonstrates that HashSet does not allow duplicate elements.
true true false HashSet: [Java, C++] true false HashSet: [Java, C++, C]