Chapter 11. Collections


The term "collections" refers to the notion of objects that contain or manage other objects. Most developers are familiar with the basic collections, including the classic array (such as String[]) or the built-in Java classes (such as java.util.Hashtable). The introduction of a rich Java Collections Framework in Java 2 (JDK 1.2) brought a standardized implementation of classes for expressing more complex relationships. The Apache Jakarta Commons Collections project adds even more capability, as described in this chapter.

Table 11-1 compares some of the key differences between basic collection types. When possible, you should try to rely on the interfaces shown, not on the underlying implementation. For example, even if you have marked a HashMap as typed, sorted, and unmodifiable, you should still interface with it as a java.util.Map whenever possible:

Table 11-1. Collection Features
 

Interface

Duplicates

Keys

Indexed

Ordered

Array

Object[]

Yes

No

Yes

Yes

Bag

org.apache.commons
.collections.Bag

Yes

No

No

No

List

java.util.List

Yes

No

Yes

Yes

Map

java.util.Map

No

Yes

No

Optional

Primitive Array

primitive[]

Yes

No

Yes

Yes

Set

java.util.Set

No

No

No

Optional


 java.util.Map myMap = new java.util.HashMap(); 

For more information on the built-in JDK 1.4 collections, see http://java.sun.com/j2se/1.4.2/docs/guide/collections/reference.html. Note that this chapter does not duplicate information included in the default JDK documentationfor example, the Stack and SortedMap interfaces are concepts covered as part of the standard Java collection interface.

The meaning of each column heading in Table 11-1 is as follows:

Interface: The expected Java interface for this collection type.

Duplicates: Are duplicate elements allowed?

Keys: Are elements accessible by a key value (e.g., java.util.Hashtable)?

Indexed: Is the order of the elements maintained by a column in the database?

Ordered: Are the results returned as a sorted collection?



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net