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:
|
Method
|
Uses
|
|
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:
HashSet
TreeSet
LinkedHashSet
HashSet:
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.
TreeSet:
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.
LinkedHashSet:
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[]={34, 22,10,60,30,22};
Set<Integer> set = new HashSet<Integer>();
try{
for(int i=0; i<5; i++){
set.add(count[i]);
}
System.out.println(set);
TreeSet sortedSet=new TreeSet<Integer>(set);
System.out.println("The sorted list is:");
System.out.println(sortedSet);
System.out.println("The First element of the set is:
"+
(Integer)sortedSet.first());
System.out.println("The last element of the set is:
"+
(Integer)sortedSet.last());
}
catch(Exception e){}
}
}
|
Output of the Program:
C:\nisha>javac SetDemo.java
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
C:\nisha> |
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:

|