Java Reference
In-Depth Information
•
Do you plan to search the data? If so, how? If you want to examine elements in
order of insertion, you want a list. If you intend to search for arbitrary elements,
a set may be better. If you need to find an object, given partial information about
it (for example, a PIN or ID number, to find a user's bank account object), a map
may be best.
•
In what order should the elements be stored? Lists hold their elements in order of
insertion, while the tree collections (
TreeSet
and
TreeMap
) order elements by
their natural ordering. If order doesn't matter, you may want a hash table collec-
tion such as a
HashSet
.
Chapter Summary
A collection is an object that stores a group of other
objects. Examples of collections are
ArrayList
,
HashSet
, and
TreeMap
. Collections are used to
structure, organize, and search data.
A set is a collection that doesn't allow duplicates. Sets
generally can be searched very quickly to see whether they
contain a particular element value. The
Set
interface rep-
resents sets.
A linked list is a collection that's similar to an
ArrayList
but that is implemented internally by storing each element
in a small container object called a node. Linked lists can
perform certain operations faster than array lists, such as
adding data to and removing data from the front or middle
of the list.
There are two major set classes in Java:
TreeSet
and
HashSet
. A
TreeSet
holds
Comparable
data in a sorted
order; a
HashSet
can hold any data and can be searched
faster, but its elements are stored in an unpredictable
order.
A map is a collection that associates key objects with
value objects. Maps are used to create relationships of
association between pieces of data, such as a person's
name and phone number.
An iterator is an object that keeps track of the current
position in a list and expedites the examination of its ele-
ments in sequential order. Linked lists are often used with
iterators for increased efficiency.
There are two major map classes in Java:
TreeMap
and
HashMap
. A
TreeMap
holds
Comparable
keys in a sorted
order; a
HashMap
can hold any data as its keys and per-
forms value lookups faster, but its keys are stored in an
unpredictable order.
An abstract data type (ADT) is a specification of the oper-
ations that can be performed on data. Two examples of
ADTs are
List
and
Set
. ADTs in the Java Collections
Framework are represented as interfaces (e.g., the
List
interface, which is implemented both by
LinkedList
and
by
ArrayList
).
Search WWH ::
Custom Search