Set Interface

public interface Set extends Collection
The Set interface extends Collection interface but does not add any additional methods. A set represents a non-indexed collection of unique elements.
Set interface features:
  • It does not allow duplicate elements.
  • A set can contain at most one null value.
  • The Set interface does not add any additional new methods.
  • It is an unordered collection of distinct objects.
Example of Set interface
import java.util.Set; import java.util.HashSet; import java.util.Iterator; public class SetDemo { public static void main(String ar[]) { // Create a set Set set= new HashSet(); //Adding elements to the set set.add("Core java"); set.add("Struts"); set.add("Spring"); set.add("j2ee"); set.add("EJB"); set.add("Hibernate"); set.add("Core java"); // Find the size of set System.out.println("There are "+set.size()+" element in the set"); System.out.println("contents of set are......."); // Display set element using iterator method Iterator itr= set.iterator(); while(itr.hasNext()) { System.out.println(; } System.out.println("Removing EJB from the set....."); set.remove("EJB"); System.out.println("No. of elements after removal :"+ set.size()); System.out.println("Searching struts in the set ........"); System.out.println("Searching element is :" + set.contains("Struts")); } }

There are 6 element in the set
contents of set are.......
Core java
Removing EJB from the set.....
No. of elements after removal :5
Searching struts in the set ........
Searching element is :true

Download this example


Latest Tutorial

Most Visited Tutorial