USING JAVA COLLECTIONS

The Collection interface is a new feature of
JDK(tm) 1.2.  A "collection" in data structure terms is a group of
elements.  Some types of collections that derive from Collection include
the following:

   List -- an interface specifying an ordered sequence of elements

   Set -- an interface specifying a mathematical set of elements, with no
   duplication

   Vector -- an implementation of List


Specifying interfaces in this way lets you manipulate collections without
having to know the details of how a given collection is implemented.  Note
that existing data types like Vector have been incorporated into the
Collection framework.


To see why the collections approach can be useful, consider a simple
example, that of sorting a vector of elements.  JDK 1.1 and earlier
versions offered no standard way to do this kind of sorting, but
collections provides the capability.  Consider the following example:

        import java.util.*;
        
        class Comparer implements Comparator {
                public int compare(Object obj1, Object obj2)
                {
                        int i1 = ((Integer)obj1).intValue();
                        int i2 = ((Integer)obj2).intValue();
        
                        return Math.abs(i1) - Math.abs(i2);
                }
        }
        
        public class collect {
                public static void main(String args[])
                {
                        Vector vec = new Vector();
        
                        vec.addElement(new Integer(-200));
                        vec.addElement(new Integer(100));
                        vec.addElement(new Integer(400));
                        vec.addElement(new Integer(-300));
        
                        Collections.sort(vec);
                        for (int i = 0; i < vec.size(); i++) {
                                int e=((Integer)vec.elementAt(i)).intValue();
                                System.out.println(e);
                        }
        
                        Collections.sort(vec, new Comparer());
                        for (int i = 0; i < vec.size(); i++) {
                                int e=((Integer)vec.elementAt(i)).intValue();
                                System.out.println(e);
                        }
                }
        }

In this example, there is a vector of Integer wrappers on integral
quantities.  Calling Collections.sort on this vector sorts the vector in a
natural way, that is, from lowest to highest according to the values
contained in the wrappers.  This sorting is achieved by virtue of the fact
that all the wrapper types, along with String and a few others, implement
an interface called java.lang.Comparable that imposes an ordering on
individual elements.  And Vector is an implementation of List, which
defines a sorting method on lists.

But suppose you want to sort this vector according to your own ordering,
one that compares elements according to their absolute magnitude.  How can
you do this?

One approach is to call Collections.sort again, this time specifying your
own ordering.  This ordering is done by defining a class "Comparer" that
implements the java.util.Comparator interface, returning < 0, 0, or > 0
according to whether the first element is less than, equal to, or greater
than the second.  The difference between java.lang.Comparable and
java.util.Comparator is simply that Comparable is used for types (such as
Integer and String) that are built in to the core API, while Comparator is
used in cases where you want to specify your own custom element ordering.

Also, there are other features in collections worth investigating, such as
the Map interface that replaces Dictionary and binary searches on ordered
lists.  The benefit of the collection framework is that it provides easier
and more powerful use of Java language data structures.