//Creating HashSet
HashSet<String> set = new HashSet();
//adding elements
set.add("One");
set.add("Two");
//Removing element);
set.remove("One");
//Removing all the elements available in the set
set.clear();
// Set implementation using HashSet
Set<String> animals = new HashSet<>();
Set<T> mySet = new HashSet<>();
// A Java program to demonstrate a Set.
// Here, you will see how you can add
// Elements using Set.
import java.util.*;
public class SetExample {
public static void main(String[] args)
{
// Set demonstration using HashSet
Set<String> Set = new HashSet<String>();
// Adding Elements
Set.add("one");
Set.add("two");
Set.add("three");
Set.add("four");
Set.add("five");
// Set follows unordered way.
System.out.println(Set);
}
}
A Set is a Collection that cannot contain duplicate elements.
The Set interface contains only methods inherited from Collection
and adds the restriction that duplicate elements are prohibited.
public E set(int index, E element)
//The set() method of
//java.util.ArrayList class is used to replace the
//element at the specified position in this list with
//the specified element.