Set have its implementation in various classes like HashSet , TreeSet , LinkedHashSet .
// Hashset Random SortingSet<T> set =newHashSet<T>();// TreeSet - By compareTo() or ComparatorTreeSet<T> sortedSet =newTreeSet<T>();// LinkedHashSet - Insertion OrderLinkedHashSet<T> linkedhashset =newLinkedHashSet<T>();
Creating a set
Set<Integer> set =newHashSet<Integer>();// Creates an empty Set of IntegersSet<Integer> linkedHashSet =newLinkedHashSet<Integer>(); //Creates a empty Set of Integers, with predictable iteration order
Adding elements to a Set
set.add(12);
set.add(13);
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
Check size
set.size();
//Returns the number of elements in the collection
set.isEmpty();
//Returns true if the set has no elements