Java Collections Framework COMP53 Oct 24, 2007
Collections Framework A unified architecture for representing and manipulating collections Allows collections to be manipulated independently of the details of their representation. Reduces programming effort while increasing performance. Reduces effort in designing and learning new APIs, and fosters software reuse. Based on six collection interfaces and algorithms to manipulate them.
Core Collection Interfaces These are the interfaces that define the operations that are expected for various kinds of collections. This is an inheritance hierarchy. Collection SetListQueue Map SortedMap SortedSet
Core Collections Collection: the root of the collection hierarchy. A collection represents a group of objects known as its elements. Set: a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction. List: an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. Queue: a collection used to hold multiple elements prior to processing. Map: an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. SortedSet and SortedMap: Same functionality as Set and Map, but elements are ordered.
Core Collection Interfaces These are the interfaces that define the operations that are expected for various kinds of collections. This is an inheritance hierarchy. Collection SetListQueue Map SortedMap SortedSet For the moment, we’ll focus on the collections we know. We’ll return to the others later.
The Collection Interface public interface Collection extends Iterable { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean containsAll(Collection c); Iterator iterator(); These operations are required. All collections must implement them.
The Collection Interface public interface Collection extends Iterable { boolean add(E element); boolean remove(Object element); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); These operations are optional – collections may leave them unimplemented. Unimplemented methods must throw UnsupportedOperationException.
The Collection Interface public interface Collection extends Iterable { // Array operations Object[] toArray(); T[] toArray(T[] a); These operations are also required. Every collection must be convertible to an array.
The List Interface public interface List extends Collection { // Positional access E get(int index); E set(int index, E element); boolean add(E element); void add(int index, E element); E remove(int index); boolean addAll(int index, Collection c); get() is required, others are optional.
The List Interface public interface List extends Collection { // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); These operations are required.
The Queue Interface public interface Queue extends Collection { boolean add(E element); // from Collection E remove(); E element(); boolean offer(E e); E poll(); E peek(); } First three throw exceptions on failure. Last three return false or null on failure.
Stack and Queue Operations JCFSTACK ADTQUEUE ADT add offer pushenqueue remove poll popdequeue element peek topfront
Legacy Collections These collections from older versions of Java are still available: Vector – old-style List HashTable – old-style Map
List Implementations ArrayList and LinkedList both implement the List interface – as defined in sections 3.1,3.2 and previous lecture Most of the time, use ArrayList, which offers constant-time positional access and is just plain fast. If you frequently add elements to the beginning of the list or iterate to delete elements from list’s interior, consider using LinkedList.