Set Interface

The Set interface extends the Collection interface.

Set Interface


The Set interface extends the Collection interface. It neither contains duplicate elements nor  maps a key value to an object. It permits a single element to be null. The Set interface contains only methods inherited from Collection interface, these are shown in the table given below:



 add( )

Adds an object to the collection

 clear( )

Removes all objects from the collection

 contains( )

Returns true if a specified object is an element within the collection

 isEmpty( )

Returns true if the collection has no elements

 iterator( )

Returns an Iterator object for the collection which may be used to retrieve an object

 remove( )

Removes a specified object from the collection

 size( )

Returns the number of elements in the collection

In the Java platform, Collections Framework provides three general-purpose Set implementation-classes: 


This is a class which stores its elements in a hash table and doesn't allow to store duplicate collections. This class permits the null element and is used to perform the basic operations such as add, remove, contains and size. This is the best way to perform set implementation.

The TreeSet implementation is useful when you need to extract elements from a collection in a sorted manner. The elements added to a TreeSet are sortable in an order. It is generally faster to add elements to a HashSet, then converting the collection to a TreeSet for sorted traversal.

It is a class which is implements both the Hash table and linked list implementation of the Set interface. This implementation differs from HashSet that it maintains a doubly-linked list. The orders of its elements are based on the order in which they were inserted into the set (insertion-order). 

SortedSet Interface:

The SortedSet interface extends the Set interface. It maintains its elements in ascending order.  It neither contains duplicate elements nor  maps a key value to an object. It permits a single element to be null. In addition to methods of  the Set interface, it also provides two following methods:

 first( )
 last( ) 

The first( ) method returns the first (lowest) element currently in the collection while the last( ) method returns the last (highest) element currently in the collection.

Let see an example that stores a group of numbers to the Hash table using HashSet class.

import java.util.*;

public class SetDemo {
  public static void main(String args[]) { 
  int count[]={3422,10,60,30,22};
 Set<Integer> set = new HashSet<Integer>();
  for(int i=0; i<5; i++){
  TreeSet sortedSet=new TreeSet<Integer>(set);
  System.out.println("The sorted list is:");

"The First element of the set is: "+
"The last element of the set is: "+

  catch(Exception e){}

Output of the Program:


C:\nisha>java SetDemo
[34, 22, 10, 30, 60]
The sorted list is:
[10, 22, 30, 34, 60]
The First element of the set is: 10
The last element of the set is: 60


This program creates a HashSet and adds a group of numbers, including a number "22" twice. The program than prints out the list of numbers in the set, note that the duplicate number is not added to the set. Then the program treats the set as a TreeSet and displays the sorted list of the set. The first( ) and the last( ) methods display the first & last elements of the set respectively.

Download this Program:




We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Set Interface

Posted on: May 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Set Interface   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Prince Adeleye
April 6, 2011

This is what I've been looking for... I love this, great work! keep it up.