Make your own free website on Tripod.com
 The Java Explorer Back | TOC | Next

<Core><Intermediate><Advanced>
Overview | Packages | Class Internals | Collections | I-O | Network | Database 

The collections framework in Java evolved over time. With an improved Vector and Hashtable classes, Java Foundation Classes (JFC) added an impressive set of API for handling data structures such as List, Map and Set.

The framework provides methods to manipulate data via standard and uniform interfaces. Early Java programmers are already familiar with manipulating data in Hashtable with Enumeration interface. In the collections framework, you have an Iterator class to loop over the elements of the collection object, and perform other operations such as insert, delete, or peek ahead. It is an improved and advanced version of the Enumeration interface.

The Iterator class in the collections framework exemplifies the Iterator design pattern. [ Watch this site for a discussion of the Iterator pattern under the patterns category. ]

The collections framework is a hierarchy of interfaces that can be manipulated independently of their representations. Here is a pictorial representation of the collections framework.

     Collection     
| |
Set List
|
SortedSet

Map
|
SortedMap

You will notice that there are two hierarchies, one rooted at Collection interface and the other at Map interface. Obviously, Map is not a collection because it abstracts the notion of name-value pairs, or an associative array, if you prefer. The Collection, on the other hand, is just what it says; it represents a collection of elements.

A Collection stores a group of objects called elements, which may be ordered or unordered, and a particular implementation (Set or List) may or may not allow duplicate elements in a Collection.

When you want a Collection that does not allow duplicate elements, then you want a Set. You use a SortedSet when you want the elements sorted in ascending order. 

If you want an ordered collection, but don't care about duplicate entries, then you want a List. A Vector is one representation of a List. With a List object, you can iterate over the elements using an integer index.

A Map allows you to map keys to values, but doesn't allow duplicates. Every key is mapped to a single value, that is, it is a one-to-one association. A SortedMap stores key-value pairs in an ascending key order.

Where do you use which Collection?

Collection Type Element Ordering Duplicates Implementation Where Used
List Ordered Allowed ArrayList

LinkedList

Store inventory items
Use AarayList where you would normally use a Vector and LinkedList for queues.
Set Ordered Not Allowed HashSet

TreeSet

Student's course schedule
Map UnOrdered
Use SortedMap for ordered keys
Does not allow duplicate keys HashMap

TreeMap

Name vs Phone Numbers

There are many other data structures that you may want to use when your program demands: Linked List, the many variations of Trees, and so on. It is possible to build new data structures on these base implementations, or derive new semantics from existing ones.

The implementation column in the table above shows that Java provides two concrete classes for each interface. Let us take them one by one.

The List interface has ArrayList and LinkedList. ArrayList is suitable for use as a Vector, and LinkedList for queues and dequeues.

To be continued...