public
boolean add(E e)
Adds
the specified element to this set if it is not already present.
Return true if this set did not already contain the specified
element.
import java.util.*; class HashSetAdd{ public static void main(String args[]){ HashSet<Integer> mySet = new HashSet<> (); /* Add Elements to HashSet */ mySet.add(10); mySet.add(20); mySet.add(30); mySet.add(40); System.out.println("Elements in HashSet are"); System.out.println(mySet); System.out.print("\nIs 50 added to set "); System.out.println(mySet.add(50)); System.out.println("\nElements in HashSet are"); System.out.println(mySet); System.out.print("\nIs 10 added to set "); System.out.println(mySet.add(10)); System.out.println("\nElements in HashSet are"); System.out.println(mySet); } }
Output
Elements in HashSet are [20, 40, 10, 30] Is 50 added to set true Elements in HashSet are [50, 20, 40, 10, 30] Is 10 added to set false Elements in HashSet are [50, 20, 40, 10, 30]
No comments:
Post a Comment