Set Interface

A Set is a collection that cannot contain duplicate elements. This is identical to the mathematical definition of a set. All the methods in the Set interface are inherited from the Collection interface. Two classes in the java.util package implement the Set interface: HashSet and TreeSet. HashSet is faster but does not guarantee ordering; if ordering is required, TreeSet should be used. TreeSet is a sorted set.

CollectExample adds an number of elements to a HashSet, then prints the set.

CollectExample

import java.util.*;

public class CollectExample
{

public static void main(String[] args)
{
Collection c = new HashSet();
c.add(''Smith");
c.add("Jones");
c.add("Smith");

c.add(''Brown");
c.add("Able");
Iterator i = c.iterator();
while (i.hasNext() ) {
System.out.print(i.next() + " ");
}
}
}


The output from CollectExample is

> java CollectExample
Jones Smith Brown Able


Note that since duplicates are not allowed in sets, the set contains only one element named ''Smith". Note (line 6) that we have defined our HashSet as a Collection type, c.
We could have defined the HashSet as a Set type, as follows:

Set c = new HashSet();

However, as we shall see, using a Collection type makes writing generic code easier. A HashSet has a backing hash table of default initial capacity of 16 buckets (in J2SE 1.4) and load factor of 0.75. This means the hash table is allowed to become three quarters full before it is increased. We can change these default tuning parameters by the constructors HashSet(int capacity) and HashSet(int capacity, float load_factor).

Elements are added to the set using the add method. The Iterator object, which is part of the Collection interface, is used for traversing over collections. The Iterator has three associated methods.

hasNext() returns true if the iteration has more elements
next() returns the next element in the iteration
remove() removes the current element in the iteration from the collection


The program uses the hasNext and next methods to print the entire collection. The following code fragment is used to remove "Brown" from the collection using the Iterator remove method. Note that the Iterator next method returns an Object type.

Iterator i = c.iterator();
while (i.hasNext() )
{
Object o = i.next();

if (o.equals("Brown") )
{
i.remove();
}
}


We could remove "Brown" from the collection using the Collection remove method,as follows:

c.remove("Brown");

The Collection interface has a number of methods for performing bulk operations such as addAll and removeAll. For example, suppose we create a second collection c2, as follows:

Collection c2 = new HashSet();
c2.add("Thomas");

c2.add("Able");
The statement
c.addAll(c2);


adds the contents of c2 to c. This is equivalent to a mathematical set union. The resulting contents of c will be ''Smith", "Jones", "Brown", "Able", and "Thomas". The statement

c.removeAll(c2);

removes the contents of c2 from c, or put another way, the result is the set of elements in c that are not in c2. This is equivalent to a mathematical set difference. The resulting contents of c will be "Smith", "Jones", and "Brown".

No comments:

Post a Comment