Download Java Collection Framework

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
School of Informatics, University of Edinburgh
Computer Science 1 Ah
CS1Ah Lecture Note 23
The Java Collection Framework
23.1
Introduction
Recent lectures have introduced a number of different kinds of container or collection
classes, for example, the Vector, LinkedList and TreeSet classes. Each object in a
collection class contains a finite set of other objects, usually referred to as its elements.
Different kinds of collections have different properties: for example, elements might
be maintained in order, or duplicate elements may be forbidden, and the efficiency of
methods varies.
This note explores how collection classes in Java are arranged in a collection framework and what the benefits are to doing this.
23.2
Interfaces
Collection classes are organised according to their interfaces. An interface can be
thought of as the skeleton of a class: it specifies the types of the methods a class is
expected to define, but gives no information on how the methods are implemented, or
what fields the class should define. We say that a class implements an interface when
it includes the methods specified by the interface. It may also include other methods.
In general, a class may implement more than one interface.
Java supports the description of interfaces. These interfaces can be organised in
tree-shaped inheritance hierarchies, much as classes are. The formal definition of
interfaces in Java is covered in CS1Bh.
The interfaces in Java’s collection framework and their inheritance relationships are
shown in figure 23.1.
Collection
/
\
List
Set
|
SortedSet
Figure 23.1: Interfaces of Java collection classes
1
School of Informatics, University of Edinburgh
Computer Science 1 Ah
The interfaces are summarised below:
Collection: Includes methods for size, adding, removing and checking membership of
elements, intersection (retainAll()), union (addAll()), difference (removeAll()),
copying (cloning), comparing for equality, creating iterators.
Set: For unordered collections with no duplicates. Has same methods as Collection,
but all methods must preserve the ‘no duplicates’ property.
SortedSet: Has extra methods to select smallest and largest elements, and elements
within given ranges.
List: For finite sequences. Duplicates are fine. Adds methods for adding, removing,
getting and setting elements at positions in sequence specified by indexes.
These interfaces are frequently used when designing software components that exchange collections. They are used as the argument types or return types of methods on
the public boundaries of the components in place of collection implementation classes.
Their use hides the choice of implementation within each component, and gives the implementors of each component the opportunity to change collection implementations
without having to change the code at the boundaries between components.
23.3 Implementations
Here is a summary of the main collection implementations in Java. Methods are
grouped according to their time complexity using ‘big-O’ or ‘order’ notation to indicate how their worst-case execution time varies with size n of collection. For example,
O(log n) indicates that the worst-case execution time varies with the log (conventionally
base 2) of the collection size.
LinkedList: Implements List interface.
O(1) get, set, add, remove at first and last positions, and at positions given by
iterators.
O(n) add or remove at arbitrary numeric index or of given element, contains
given element.
ArrayList, Vector: Both implement List interface.
O(1) get, set at positions given by index or iterator, Also add, remove at last
position.
O(log n) contains given element if sorted. Otherwise O(n).
O(n) add, remove at arbitrary index or of given element.
Vector is older and ArrayList is recommended for most cases. See Java doc for
differences.
arrays: Implements List interface when asList method of java.util.Arrays class
used to provide List view.
Complexities as with ArrayList, except that, since size fixed, add and remove
are not defined.
2
School of Informatics, University of Edinburgh
Computer Science 1 Ah
TreeSet: Implements SortedSet interface. Elements in order.
O(log n) guaranteed for add, remove, of given element, contains test, get, add,
remove of greatest or least element.
HashSet: Implements Set interface. Elements not in order.
O(1) usually for add, remove of given element, contains test. Constant factors in
time complexity can be significant. Factors tunable.
The implementation relationship respects the inheritance of interfaces. For example, the HashSet class implements the Set interface which inherits from the Collection
interface, so HashSet implements the Collection interface.
23.4
Algorithms
The java.util.Collections class provides several static methods that operate on
collections. A few are summarised below. All work on any class with a List interface,
except min and max which work on any class with a Collection interface.
sort: Arrange elements in ascending order. Algorithm guaranteed to run in O(n log n)
time.
shuffle: Does opposite of sort: reorder elements, based on input from a source of
randomness.
binarySearch: Find whether list contains a given element. Assumes list elements are
in sorted order. Has O(log n) efficiency only if list implementation supports O(1)
access by position index.
min/max: Find least or greatest element of the collection. Simple O(n) implementations.
In addition, the Collections class provides several methods for changing the property
of a collection object. For example, it can make a collection object ‘read only’, such that
any attempt to modify the collection results in an exception being raised.
23.5
Iterators
Previous notes have introduced iterators as objects that support next and hasNext
methods and that can be used to sequence through the elements of particular collections. Since an iterator generation method is included in the top-level Collection
interface, every collection class must provide an iterator implementation. Iterators
therefore are a truly generic way of sequencing through any collection.
Every class implementing the List interface also provides richer ListIterators,
which allow for both insertion and deletion of elements, and motion in both directions
along lists.
3
School of Informatics, University of Edinburgh
Computer Science 1 Ah
23.6 Ordering Collection Elements
Several collection classes and algorithms need to know about how to order elements,
for example, the TreeSet class, or the sort algorithm. The default way they do this is
by calling a compareTo method of elements. Therefore the classes used for elements
in these cases must define this method. All the numeric classes in the Java library
provide a compareTo method. Alternatively, these classes and algorithms permit the
explicit provision of an ordering relation.
23.7 Maps
Closely related to collections are maps: sets of entries, each entry comprising a key and
a value, with the restriction that no two distinct entries may have equal keys. Maps
provide methods for adding new entries and looking up the entry for a given key. The
Java collections framework defines Map and SortedMap interfaces, and TreeMap and
HashMap implementation classes.
The ClassOfStudents example from previous lectures has the flavour of a map
since entries could be looked up using surnames as keys.
23.8 Benefits
When programming in any language, not just Java, there are strong benefits to using
a collection framework such as the one just described. The use of a framework:
• reduces programming effort: Less time is spent reinventing the wheel.
• provides standards for collection exchange: Interactions between software components frequently involve exchanges of various types of collections. Standards
ensure predictable behaviour and eliminate need to write collection translation
code.
• reduces learning time: since all collections have similar interfaces.
• speeds design of new implementations: because interfaces provide framework
for design.
• eases experimentation with different implementations: when code using collection objects only assumes they implement given interfaces.
In this course, we have given ad-hoc examples of collections for pedagogical reasons.
But when programming for real, it is almost always best to use an existing collection framework. This doesn’t restrict you to using the given implementations: you
can always design your own implementations, just make sure they implement one the
standard framework interfaces.
For further information see http://java.sun.com/j2se/1.4/docs/guide/collections/.
For example look up descriptions of methods described informally in this note. Also
many Java text-books have one or more chapters on collections.
Paul Jackson, 2002/11/28 09:57:53.
4