Collections Framework

  • A collections framework is a unified architecture for representing and manipulating collections.
  • All collections frameworks contain the following: interfaces, implementations, algorithms.
  • The Java Collections Framework provides the following benefits: reduces programming effort, increases program speed and quality, allows interoperability among unrelated APIs, reduces effort to learn and to use new APIs, reduces effort to design new APIs, fosters software reuse.
  • A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group.

Custom Collection Implementations

//How to write a custom implementation:

public static <T> List<T> asList(T[] a) {
    return new MyArrayList<T>(a);
}

private static class MyArrayList<T> extends AbstractList<T> {

    private final T[] a;

    MyArrayList(T[] array) {
        a = array;
    }

    public T get(int index) {
        return a[index];
    }

    public T set(int index, T element) {
        T oldValue = a[index];
        a[index] = element;
        return oldValue;
    }

    public int size() {
        return a.length;
    }
}

The following list illustrates the sort of custom Collections you might want to implement: persistent, application-specific, high-performance+special-purpose, high-performance+general-purpose, enhanced functionality, convenience, adapter.

The process of writing a custom implementation follows: 1 - choose the appropriate abstract implementation class from the preceding list; 2 - provide implementations for all the abstract methods of the class; 3 - test and, if necessary, debug the implementation;4 - if you are concerned about performance, read the API documentation of the abstract implementation class for all the methods whose implementations you're inheriting.

Collections Framework — Structure map

Clickable & Draggable!

Collections Framework — Related pages: