Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Get started with the Java Collections Framework

Find out how Sun's new offering can help you to make your collections more useful and accessible

  • Print
  • Feedback

Page 2 of 5

  • A basic set of collection implementations

    In addition to the trusty Hashtable and Vector, which have been updated to implement the Collection interfaces, new collection implementations have been added, including HashSet and TreeSet, ArrayList and LinkedList, and HashMap and Map. Using an existing, common implementation makes your code shorter and quicker to download. Also, using existing Core Java code core ensures that any improvements to the base code will also improve the performance of your code.

  • Other useful enhancements

    Each collection now returns an Iterator, an improved type of Enumeration that allows element operations such as insertion and deletion. The Iterator is "fail-fast," which means you get an exception if the list you're iterating is changed by another user. Also, list-based collections such as Vector return a ListIterator that allow bi-directional iteration and updating.

    Several collections (TreeSet and TreeMap) implicitly support ordering. Use these classes to maintain a sorted list with no effort. You can find the smallest and largest elements or perform a binary search to improve the performance of large lists. You can sort other collections by providing a collection-compare method (a Comparator object) or an object-compare method (the Comparable interface).

    Finally, a static class Collections provides unmodifiable (read-only) and synchronized versions of existing collections. The unmodifiable classes are helpful to prevent unwanted changes to a collection. The synchronized version of a collection is a necessity for multithreaded programs.



  • The Java Collections Framework is part of Core Java and is contained in the java.util.collections package of JDK 1.2. The framework is also available as a package for JDK 1.1 (see Resources).

    Note: The JDK 1.1 version of collections is named com.sun.java.util.collections. Keep in mind that code developed with the 1.1 version must be updated and recompiled for the 1.2 verson, and any objects serialized in 1.1 cannot be deserialized into 1.2.

    Let us now look more closely at these advantages by exercising the Java Collections Framework with some code of our own.

    A good API

    The first advantage of the Java Collections Framework is a consistent and regular API. The API is codified in a basic set of interfaces, Collection, Set, List, or Map. The Collection interface contains basic collection operations such as adding, removing, and tests for membership (containment). Any implementation of a collection, whether it is one provided by the Java Collections Framework or one of your own creations, will support one of these interfaces. Because the Collections framework is regular and consistent, you will learn a large portion of the frameworks simply by learning these interfaces.

    Both Set and List implement the Collection interface. The Set interface is identical to the Collection interface except for an additional method, toArray, which converts a Set to an Object array. The List interface also implements the Collection interface, but provides many accessors that use an integer index into the list. For instance, get, remove, and set all take an integer that affects the indexed element in the list. The Map interface is not derived from collection, but provides an interface similar to the methods in java.util.Hashtable. Keys are used to put and get values. Each of these interfaces are described in following code examples.

    • Print
    • Feedback

    Resources
    • Download the Java Collections Framework as package for Java 1.1 JDK 1.1 at the Sun JavaBeans InfoBus page http://java.sun.com/beans/infobus/index.html
    • Sun's overview and description of the Java Collections Framework for Java 1.2 http://java.sun.com/products/jdk/1.2/docs/guide/collections/index.html
    • Annotated reference of the Java Collections Framework http://java.sun.com/products/jdk/1.2/docs/guide/collections/reference.html
    • A frequently asked questions file for the Java Collections Framework. http://java.sun.com/products/jdk/1.2/docs/guide/collections/designfaq.html
    • Read about ObjectSpace's Java Generic Library -- the main competitor to the Java Collections Framework http://www.objectspace.com/products/jgl/