Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
IBM : developerWorks : Java : Education - online courses
Java Collections Framework
Download tutorial zip fileView letter-sized PDF fileView A4-sized PDF fileE-mail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPreviousNext
3. Collection interfaces and classes
  


Collection interface page 2 of 8


The Collection interface is used to represent any group of objects, or elements. You use the interface when you wish to work with a group of elements in as general a manner as possible. Here is a list of the public methods of Collection in Unified Modeling Language (UML) notation.

UML Diagram for Collection Interface

The interface supports basic operations like adding and removing. When you try to remove an element, only a single instance of the element in the collection is removed, if present.

  • boolean add(Object element)
  • boolean remove(Object element)

The Collection interface also supports query operations:

  • int size()
  • boolean isEmpty()
  • boolean contains(Object element)
  • Iterator iterator()

Iterator interface

The iterator() method of the Collection interface returns an Iterator. An Iterator is similar to the Enumeration interface, which you may already be familiar with, and will be described in Enumeration interface. With the Iterator interface methods, you can traverse a collection from start to finish and safely remove elements from the underlying Collection:

UML Diagram for Iterator Interface

The remove() method is optionally supported by the underlying collection. When called, and supported, the element returned by the last next() call is removed. To demonstrate, the following code shows the use of the Iterator interface for a general Collection:

Collection collection = ...;
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
  Object element = iterator.next();
  if (removalCheck(element)) {
    iterator.remove();
  }
}

Group operations

Other operations the Collection interface supports are tasks done on groups of elements or the entire collection at once:

  • boolean containsAll(Collection collection)
  • boolean addAll(Collection collection)
  • void clear()
  • void removeAll(Collection collection)
  • void retainAll(Collection collection)

The containsAll() method allows you to discover if the current collection contains all the elements of another collection, a subset. The remaining methods are optional, in that a specific collection might not support the altering of the collection. The addAll() method ensures all elements from another collection are added to the current collection, usually a union. The clear() method removes all elements from the current collection. The removeAll() method is like clear() but only removes a subset of elements. The retainAll() method is similar to the removeAll() method but does what might be perceived as the opposite: it removes from the current collection those elements not in the other collection, an intersection.

The remaining two interface methods, which convert a Collection to an array, will be discussed in Converting from new collections to historical collections.

AbstractCollection class

The AbstractCollection class provides the basis for the concrete collections framework classes. While you are free to implement all the methods of the Collection interface yourself, the AbstractCollection class provides implementations for all the methods, except for the iterator() and size() methods, which are implemented in the appropriate subclass. Optional methods like add() will throw an exception if the subclass doesn't override the behavior.

Collections Framework design concerns

In the creation of the Collections Framework, the Sun development team needed to provide flexible interfaces that manipulated groups of elements. To keep the design simple, instead of providing separate interfaces for optional capabilities, the interfaces define all the methods an implementation class may provide. However, some of the interface methods are optional. Because an interface implementation must provide implementations for all the interface methods, there needed to be a way for a caller to know if an optional method is not supported. The manner the framework development team chose to signal callers when an optional method is called was to throw an UnsupportedOperationException. If in the course of using a collection an UnsupportedOperationException is thrown, then the operation failed because it is not supported. To avoid having to place all collection operations within a try-catch block, the UnsupportedOperationException class is an extension of the RuntimeException class.

In addition to handling optional operations with a run-time exception, the iterators for the concrete collection implementations are fail-fast. That means that if you are using an Iterator to traverse a collection while the underlying collection is being modified by another thread, then the Iterator fails immediately by throwing a ConcurrentModificationException (another RuntimeException). That means the next time an Iterator method is called, and the underlying collection has been modified, the ConcurrentModificationException exception gets thrown.


Main menuSection menuGive feedback on this tutorialPreviousNext
PrivacyLegalContact