JAVA

Java HashSet class

Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

The important points about Java HashSet class are:

·        HashSet stores the elements by using a mechanism called hashing.

·        HashSet contains unique elements only.

Methods of Java HashSet class:

 

Method

Description

void clear()

It is used to remove all of the elements from this set.

boolean contains(Object o)

It is used to return true if this set contains the specified element.

boolean add(Object o)

It is used to adds the specified element to this set if it is not already present.

boolean isEmpty()

It is used to return true if this set contains no elements.

boolean remove(Object o)

It is used to remove the specified element from this set if it is present.

 

HashSet example program

public class HashSet{

public static void main(String args[]){

HashSet set=new HashSet();

set.add("Sindhu");

set.add("Deepthi");

set.add("Chandrakala");

set.add("Sai Charan");

set.add("Maithili");

set.add("Deepya");

set.add("Jeeshitha");

set.add("Sunitha");

Iterator itr=set.iterator();

while(itr.hasNext()){

 

System.out.println(itr.next());

}

}}

 

 


OUTPUT

Sindhu

Sai Charan

Maithili

Sunitha

Deepthi

Chandrakala

Deepya

Jeeshitha


 

Java LinkedHashSet class

Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It inherits HashSet class and implements Set interface.

The important points about Java LinkedHashSet class are:

Contains unique elements only like HashSet.

Provides all optional set operations, and permits null elements.

Maintains insertion order.

Methods of Java LinkedHashSet class:

Method

Description

void clear()

It is used to remove all of the elements from this set.

boolean contains(Object o)

It is used to return true if this set contains the specified element.

boolean add(Object o)

It is used to adds the specified element to this set if it is not already present.

 

LinkedeHashset Example program

public class Del{

public static void main(String args[]){

LinkedHashSet set=new LinkedHashSet();

set.add("Sindhu");

set.add("Deepthi");

set.add("Chandrakala");

set.add("Sai Charan");

set.add("Maithili");

set.add("Deepya");

set.add("Jeeshitha");

set.add("Sunitha");

Iterator itr=set.iterator();

while(itr.hasNext()){

System.out.println(itr.next());

}

}}


OUTPUT:

Sindhu

Deepthi

Chandrakala

Sai Charan

Maithili

Deepya

Jeeshitha

Sunitha


 

 

Java TreeSet class

Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. The objects of TreeSet class are stored in ascending order.

The important points about Java TreeSet class are:

·        Contains unique elements only like HashSet.

·        Access and retrieval times are quiet fast.

·        Maintains ascending order.

Methods of Java TreeSet class

       Method

Description

boolean addAll(Collection c)

It is used to add all of the elements in the specified collection to this set.

boolean contains(Object o)

It is used to return true if this set contains the specified element.

 

TreeSet Example program

public class Del{

public static void main(String args[]){

TreeSet set=new TreeSet();

set.add("Sindhu");

set.add("Deepthi");

set.add("Chandrakala");

set.add("Sai Charan");

set.add("Maithili");

set.add("Deepya");

set.add("Jeeshitha");

set.add("Sunitha");

Iterator itr=set.iterator();

while(itr.hasNext()){

 

System.out.println(itr.next());

}

}}


OUTPUT

Chandrakala

Deepthi

Deepya

Jeeshitha

Maithili

Sai Charan

Sindhu

Sunitha