Hints
Set interface assures all the Object taken by the add method keep it unique on the Set List. .add() method is used to add the object to the List. Here in this above example String is the Object to persist on the list.
HashSet<String>() Class provides all the definitions of the set interface.
|
UniqueSetCollectionExample
import java.util.HashSet;
import java.util.Set;
public class UniqueSetCollectionExample {
public static void main(String args[]) throws Exception{
Set<String> uniqueUserNames = new HashSet<String>();
uniqueUserNames.add("Sarathy");
uniqueUserNames.add("Gopal");
uniqueUserNames.add("Raja");
uniqueUserNames.add("Sarathy");
System.out.println(uniqueUserNames);
}
}
|
Output
[Sarathy, Raja, Gopal]
|