The Set interface in java

Set interface

A Set is a Collection that will not allow duplicate elements. If we try to pass same element then that not stored into the set. It has some following  classes.

1. HashSet class

A HashSet represent a set of elements. It does not guarantee the order of elements. It does not allows the duplicate elements. We can write HashSet class as:

class HashSet<T>

<T> represent the generic type parameter. We can create the object as:

HashSet<String> hs = new HashSet<String>();

HashSet class methods

boolean add(obj)
Adds an element to the HashSet. It returns true if the element is added, else it returns false.

void clear()
Removes all element from the HashSet.

boolean contains(obj)
Returns true if a specified element within the HashSet.

boolean isEmpty()
Returns true if the HashSet has no elements.

boolean remove(obj)
Removes a specified element from the HashSet,if it is present. It returns true if the element is removed successfully.

int size()
This returns the number of elements in the HashSet.

Write a program which shows the use of HashSet and Iterator
import java.io.*;
import java.util.HashSet;
import java.util.Iterator;
class test
{
public static void main(String args[])
{
HashSet hs = new HashSet();
hs.add(“Adam”);
hs.add(“Chris”);
hs.add(“Steve”);
hs.add(“Chris”);
System.out.println(“Hash Set=”+hs);
Iterator it = hs.iterator();
System.out.println(“Retrieving Element using Iterator: “);
while(it.hasNext())
{
String s = (String)it.next();
System.out.println(s);
}
}
}

Output:
Hash Set=[Steve, Adam, Chris] Retrieving Element using Iterator:
Steve
Adam
Chris

 

2. LinkedHashSet class

This is subclass of HashSet class and does not contain any additional members on its own. This is declared as:

class LinkedHashSet<T>

3. TreeSet

A sorted set implemented using a balanced binary tree. It is slower to search & modify than HashSet. But it always keeps the elements sorted.

We can write TreeSet class as:

class TreeSet<T>

We can create object of TreeSet as:

TreeSet<String> ts=new TreeSet<String>();

TreeSet class methods

void add(Object obj)
It adds the specified element to this set if it is not already present.

void clear()
It removes all of the elements from this set.

boolean contains(Object obj)
It returns true if this set contains the specified element.

boolean isEmpty()
It returns true if this set contains no elements.

boolean remove(Object obj)
It removes the specified element from this set if it is present.

int size()
It returns the number of elements in this set (its cardinality).

Write a program that shows the use of TreeSet class
import java.util.*;
class test
{
public static void main(String args[])
{
TreeSet<String> ts=new TreeSet<String>();
ts.add(“Adam”);
ts.add(“Sam”);
ts.add(“Mat”);
ts.add(“Sachin”);
Iterator itr=ts.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}

Output:
Adam
Mat
Sachin
Sam

Leave a Reply

Your email address will not be published. Required fields are marked *