Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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:

 

 

 

 

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[]={3422,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:

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

you people are awesome..
u r doing a exllent job.
thanks for helping me
im regular user to watch your site...

have nice day...
keep roking.......

Posted by noor on Friday, 11.14.08 @ 00:05am | #81663

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.