The add() method of Set in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function return False if the element is already present in the Set.
boolean add(E element) Where, E is the type of element maintained by this Set collection.
Parameters: The parameter element is of the type of element maintained by this Set and it refers to the element to be added to the Set.
Return Value: The function returns True if the element is not present in the set and is new, else it returns False if the element is already present in the set.
Below programs illustrate the use of Java.util.Set.add() method:
Set: [Set, 4, Geeks, Welcome, To]
Set: [50, 20, 40, 10, 60, 30]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.