Collections

Contact Us or call 1-877-932-8228
Collections

Collections

The Java Collections API is a set of classes and interfaces designed to store multiple objects.

There are a variety of classes that store objects in different ways:

  • Lists store objects in a specific order.
  • Sets reject duplicates of any objects already in the collection.
  • Maps store objects in association with a key, which is later used to look up and retrieve the object (note that if an item with a duplicate key is put into a map, the new item will replace the old item).

The basic distinctions are defined in several interfaces in the java.util package:

  • Collection is the most basic generally useful interface that most, but not all, collection classes implement.
    • It specifies a number of useful methods, such as those to add and remove elements, ascertain the size of the collection, determine if a specific object is contained in the collection, or return an Iterator that can be used to loop through all elements of the collection (more on this later).
    • Methods include: add(Object o), remove(Object o), contains(Object o), iterator().
    • Note that removing an element removes an object that compares as equal to a specified object, rather than by position.
    • Collection extends the Iterable interface, which only requires one method, which supplies an object used to iterate through the collection.
  • The List interface adds the ability to insert and delete at a specified index within the collection, or retrieve from a specific position.
  • The Set interface expects that implementing classes will modify the add methods to prevent duplicates, and also that any constructors will prevent duplicates (the add method returns a boolean that states whether the operation succeeded or not; i.e., if the object could be added because it was not already there).
    • Sets do not support any indexed retrieval.
  • The Map interface does not extend Collection, because its adding, removing, and retrieving methods use a key value to identify an element.
    • Methods include: get(Object key), put(Object key, Object value), remove(Object key), containsKey(Object key), containsValue(Object value), keySet(), entrySet().
    • Maps do not support any indexed retrieval.

In addition to the List, Set, and Map categories, there are also several inferences you can make from some of the collection class names:

  • A name beginning with Hash uses a hashing and mapping strategy internally, although the key values usually have no meaning (the hashing approach attempts to provide an efficient and approximately equal lookup time for any element).
  • A name beginning with Linked uses a linking strategy to preserve the order of insertion and is optimized for insertion/deletion as opposed to appending or iterating.
  • A name beginning with Tree uses a binary tree to impose an ordering scheme, either the natural order of the elements (as specified by the Comparable interface implemented by many classes including String and the numeric wrapper classes) or an order dictated by a special helper class object (a Comparator).

Several additional interfaces are used to define useful helper classes:

  • Enumeration provides a pair of methods that enable you to loop through a collection in a standardized manner, regardless of the type of collection (you test if there are more elements, and, if so, retrieve the next element).
    • This interface is less frequently used now that the following interface has been added to the API.
    • But, many of the elements in J2EE (servlets in particular) predate the Iterator concept and were specified to use enumerations, so they will still appear in new code.
  • Iterator is an improved version that allows an object to be removed from the collection through the iterator.

The following diagrams show some of the classes and interfaces that are available:

Collections API Class Hierarchy - Lists and Sets

Collections API Class Hierarchy - Maps

Next