Set in Java
 (1).png)
import
Set
Set have its implementation in various classes like HashSet
, TreeSet
, LinkedHashSet
.
// Hashset Random Sorting
Set<T> set = new HashSet<T>();
// TreeSet - By compareTo() or Comparator
TreeSet<T> sortedSet = new TreeSet<T>();
// LinkedHashSet - Insertion Order
LinkedHashSet<T> linkedhashset = new LinkedHashSet<T>();
Creating a set
Set<Integer> set = new HashSet<Integer>();
// Creates an empty Set of Integers
Set<Integer> linkedHashSet = new LinkedHashSet<Integer>();
//Creates a empty Set of Integers, with predictable iteration order
Adding elements to a Set
Delete all the elements of a Set
set.clear();
//Removes all objects from the collection.
set.remove(0);
// Removes first occurrence of a specified object from the collection