IT Community - Software Programming, Web Development and Technical Support

What is the Set interface?

This is a discussion on What is the Set interface? within the Java Programming forums, part of the Software Development category; What is the Set interface?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-25-2007, 05:55 AM
anbuchezhians anbuchezhians is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 77
anbuchezhians is on a distinguished road
Question What is the Set interface?

What is the Set interface?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 02:58 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: What is the Set interface?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-20-2007, 07:28 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Default Re: What is the Set interface?

Set Interface
Corresponds to the mathematical definition of a set (no
duplicates are allowed).
• Compared to the Collection interface
* Interface is identical.
* Every constructor must create a collection without duplicates.
* The operation add cannot add an element already in the set.
* The method call set1.equals(set2) works at follows
set1 subset of set2, and set2 subset set1

HashSet, TreeSet and LinkedHashSet implement the interface Set.

• HashSet
* Implemented using a hash table.
* No ordering of elements.
* add, remove, and contains methods constant time complexity O(c).

• TreeSet
* Implemented using a tree structure.
* Guarantees ordering of elements.
* add, remove, and contains methods logarithmic time complexity
O(log (n)), where n is the number of elements in the set.
* it is substantially slower than HashSet.

• LinkedHashSet
* Implemented using a hash table with a linked list running through it.
* Guarantees ordering of elements(insertion-order).
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-10-2007, 04:12 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default Re: What is the Set interface?

hi leoraja,
i done all type of searching but i couldnt get what exactly
treeset....so can be explain me what treeset is all about..whats is the advantage of implementing it....
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-10-2007, 04:18 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: What is the Set interface?

TreeSet
One of the Collection classes. It lets you access the elements in your collection by key, or sequentially by key. It has considerably more overhead than ArrayList or HashMap. Use HashSet when you don't need sequential access, just lookup by key. Use an ArrayList and use Arrays. sort if you just want the elements in order. TreeSet keeps the elements in order at all times. With ArrayList you just sort when you need to.

With TreeSets the key must be embedded in the object you store in the collection. Often you might have TreeSet of Strings. All you can do then is tell if a given String is in the Set. It won't find you an associated object he way a Treemap will. With a TreeMap the keys and the objects they are associated with are separate.

TreeSet and its brother TreeMap oddly have nothing to do with representing trees. Internally they use a tree organisation to give you an alphabetically sorted Set/Map, but you have no control over links between parents and children.

Internally TreeSet uses red-black trees. There is no need to presort the data to get a well-balanced tree. On the other hand, if the data are sorted (ascending or descending), it won't hurt as it does with some other types of tree.

Example:
PHP Code:
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

public class 
TreeSetTest {
  public static 
void main(String[] args) {
    
SortedSet parts = new TreeSet();
    
parts.add(new Item("Toaster"1234));
    
parts.add(new Item("Widget"4562));
    
parts.add(new Item("Modem"9912));
    
System.out.println(parts);

    
SortedSet sortByDescription = new TreeSet(new Comparator() {
      public 
int compare(Object aObject b) {
        
Item itemA = (Itema;
        
Item itemB = (Itemb;
        
String descrA itemA.getDescription();
        
String descrB itemB.getDescription();
        return 
descrA.compareTo(descrB);
      }
    });

    
sortByDescription.addAll(parts);
    
System.out.println(sortByDescription);
  }
}

class 
Item implements Comparable {
  public 
Item(String aDescriptionint aPartNumber) {
    
description aDescription;
    
partNumber aPartNumber;
  }

  public 
String getDescription() {
    return 
description;
  }

  public 
String toString() {
    return 
"[descripion=" description ", partNumber=" partNumber
        
"]";
  }

  public 
boolean equals(Object other) {
    if (
getClass() == other.getClass()) {
      
Item otherItem = (Itemother;
      return 
description.equals(otherItem.description)
          && 
partNumber == otherItem.partNumber;
    } else
      return 
false;
  }

  public 
int hashCode() {
    return 
13 description.hashCode() + 17 partNumber;
  }

  public 
int compareTo(Object other) {
    
Item otherItem = (Itemother;
    return 
partNumber otherItem.partNumber;
  }

  private 
String description;

  private 
int partNumber;

Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the use of interface? Mramesh C# Programming 5 02-08-2008 04:51 AM
What is the Servlet Interface? Arun Java Programming 2 09-10-2007 04:35 AM
Map interface in Collection Api leoraja8 Java Programming 6 09-06-2007 03:51 AM
c# interface Sathish Kumar C# Programming 2 09-02-2007 11:32 PM
Do we have interface to schedule a task like ITaskscheduler interface for desktop.... theone Mobile Software Development 1 07-24-2007 11:29 PM


All times are GMT -7. The time now is 11:29 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0