Java Set Interface And Methods

  • Set is one of the collection framework interface which extends root Collections interface.
  • Set collection can not contain duplicate elements.
  • Set interface has all methods which are inherited from Collection interface with special restriction of not allowing duplicate elements.
  • If two Set interface contains same elements then both are equal.
  • Set interface is implemented by LinkedHashSet, HashSet classes and extended by SortedSet interface which is implemented by TreeSet.
Java Set Interface hierarchy
Important Methods of Set Interface
  • boolean add(E e) : It will insert specified element in set if it is not available in set.
  • boolean addAll(Collection<? extends E> c) : It will add all elements of specified collection in set if not already present in set.
  • void clear() : It will remove all elements from set.
  • boolean contains(Object o) : It will return true if  given element is available in set.
  • boolean containsAll(Collection<?> c) : It will return true if all the elements of given collection are available in set.
  • boolean equals(Object o) :It will compare specified object with set to check equality.
  • boolean isEmpty() : It will return true if set is empty.
  • int hashCode() :It will returns hash code value for this set.
  • Iterator<E> iterator() : It will return an iterator over the elements in set.
  • boolean remove(Object o) : It will remove specified element from the set if it is present.
  • boolean removeAll(Collection<?> c) : It will remove all those elements from set which are specified in collection.
  • int size() : It will return size of Set.
Below given Set interface example will show you demo of Set interface's few important methods.

Java Set Interface Example
package JAVAExamples;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetInterfaceExample {
 public static void main(String args[]) {
  Set s1 = new HashSet();
  // Check initial size of set.
  System.out.println("Initial size of set : " + s1.size());
  // Add items in set. Try to add duplicate item in set as set can not accept duplicate items.
  s1.add("Item1");
  s1.add("Item2");
  s1.add("Item1");
  s1.add("Item3");
  s1.add("Item4");
  // Print set. It will show only 4 items as set can not hold duplicate items.
  System.out.println("Set items are : " + s1);
  // Check if set is empty. It will return false.
  System.out.println("Set is empty? : " + s1.isEmpty());

  // access set items through Iterator.
  Iterator iterator = s1.iterator();
                System.out.println("Set items are : ");
  while (iterator.hasNext()) {
   String element = (String) iterator.next();
   System.out.println(element);
  }

  // Remove item from set.
  s1.remove("Item3");
  // Set items after removing Item3 from set.
  System.out.println("Now Set items are : " + s1);
  // Get set size.
  System.out.println("Set size is : " + s1.size());
 }
}

Output :
Initial size of set : 0
Set items are : [Item1, Item2, Item3, Item4]
Set is empty? : false
Set items are : 
Item1
Item2
Item3
Item4
Now Set items are : [Item1, Item2, Item4]
Set size is : 3

This way, You can use java set to store non duplicate items.

No comments:

Post a Comment