Pages

Wednesday 6 August 2008

Sets

This interface is a member of the Java Collections Framework A collection that contains no duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above). The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.
Some set implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements.
Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception,or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the set may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.
Two Set instances are equal if they contain the same elements.

The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet HashSet, which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration. This implies that when iterating through the HashSet and for example printing out the elements in the set, there is no guarantee that the elements will be printed out in the exact order that they were entered into the HashSet It makes no guarantees as to the iteration order of the set;
in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

A simple way to remove the duplicates from a Collection c, is to enter the Collection, c, as the parameter of the HashSet constructor HashSet(Collection c)
for example Set set = new HashSet(c);
NOTE: By convention all general-purpose collection implementations have a constructor that takes a Collection argument. This constructor, known as a conversion constructor, initializes the new collection to contain all of the elements in the specified collection, whatever the given collection's subinterface or implementation type. In other words, it allows you to convert the collection's type for exaple from and Integer to String


//HashSetDemo.java

import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;

/**
*Demonstrates the use of HashSet
*/
public class HashSetDemo
{
     public static void main(String[] args)
     {
        String[] array = {"Java", "C", "C++", "SmallTalk", "C", "C++", "Delphi", "Java",
         "Lisp", "Ada", "C", "many others..."};

        Set set = new HashSet();

        //Add array elements to HashSet
        for (String item : array)
        {
           set.add(item);
        } //End of for statement block
        
        //Notice that the elements printed out are not in the order that they were
        //inserted into the array
        System.out.print(set);
 
        System.out.println();
     } //End of method main
} //End of class HashSetDemo


Below is a sample of the output of this application. It may(will) vary from the result produced on your computer
However, the main point is to illustrate that HashSet does not tolerate duplicates and it offers no guarantee on the order
of elements returned.

[SmallTalk, C, Ada, Lisp, many others..., C++, Delphi, Java]

No comments: