Java Reference
In-Depth Information
How does it do all that? Some of the increased performance of the Collections
Framework comes about because of the use of non-synchronized versions of some
of the original object container classes. For example, we've already seen the non-
synchronized HashMap class that generally replaces the original Hashtable
class when thread safety is not an issue. There is also a non-synchronized replace-
ment for Vector called ArrayList .
In addition, the Collections Framework includes optimized high-performance
implementations of several useful and important algorithms and data structures -
sorting algorithms for instance. Since these are provided by the framework, you
don't have to write them yourself.
10.6.1 Collections Framework organization
The Collections Framework adds interfaces for several new container types. The
root interface is Collection ,which represents a group of objects. There are
three main sub-interfaces - Set , List , and SortedSet .A Set is a collection
that has no duplicate elements. The order of appearance of elements in the Set
is unspecified and may change. A List is an ordered collection such that each
element in the list has a distinct place in the list. An element in a list can be
retrieved by its integer index, somewhat like an array. Most Lists permit dupli-
cate elements. A SortedSet is a Set stored in such a way that when you iterate
through the SortedSet with an Iterator (see next section), the elements are
returned in ascending “order.”
These are interfaces, not concrete classes. The Collections Framework has
several concrete implementations. For example, Stack , LinkedList , and
ArrayList all implement List .Aconcrete SortedSet is TreeSet , and
a concrete Set is HashSet . There are several other concrete implementations
as well for specialized purposes. We illustrate only a few of these in this topic.
There are two other base interfaces in the Collections Framework - Map and
SortedMap . All maps use key/value pairs, as in the Hashtable seen above.
Other concrete implementations of Map are the Properties and HashMap
classes seen earlier. A concrete version of SortedMap is the TreeMap .
J2SE 5.0 adds the Queue interface to the Collections Framework, and the
LinkedList class has been retrofitted to implement Queue . That means you
can use a LinkedList as a plain List or as a Queue . Queues are useful for
when you need first-in, first-out behavior, though some queues provide different
orderings. All Collections support add() and remove() methods to insert
and remove elements. The Queue interface adds the preferred offer() and
poll() methods. They are functionally equivalent to add() and remove()
except for their behavior in exceptional situations. While add() fails by throwing
an unchecked exception if the queue is full, offer() returns false . Similarly,
Search WWH ::




Custom Search