Set

Set is a type of Collection in java. It is similar to List but does not allow duplicate elements

to be inserted. Set is an Interface and the Concrete Implementations which uses the Set interface

is HashSet.

Example

//Create a Set Object

Set s = new HashSet();

//Add method adds a element into the Set. This method returns a boolean

//If duplicate is found, then the element is not inserted and it will return false

//if the element is unique, it will add the element to the set and returns true

bool a = s.add(new Integer(1));

if(a) { System.out.println(“Inserted”);}

else { System.out.println(“Duplicate Element. Not Inserted”);