Java Reference
In-Depth Information
Figure 7.1 shows the Collection interface and its core subinterfaces.
FIGURE 7.1 The Collection interface is the root of all collections except maps.
Collection
Map
List
Set
Queue
Maps are the only collections that do not implement the Collection interface because
elements in a map are key-value pairs of data while elements in a collection are single items.
The Map interface contains methods for working with keys and values that do not apply to
Collection objects.
The Collection interface contains useful methods for working with lists, sets, and
queues, including:
public boolean add(E e) Adds an element to the collection.
public boolean remove(Object e) Removes a single instance of the given object from
the collection.
public boolean contains(Object e) Returns true if the given Object appears in the
collection.
public Iterator<E> iterator() Returns an iterator over the elements in the collection.
All of the interfaces and classes in the Collections Framework are generics,
as evidenced by the E parameter of the add method and the <E> generic
return type of the iterator method. Generics are an important aspect of the
Collections Framework and are discussed in detail in the section, “Using
Generics,” later in this chapter.
Let's look at each of the collection types and their corresponding interfaces and classes,
starting with lists.
Lists
A list is an ordered collection that can contain duplicate entries. Items in a list can be
retrieved and inserted at a specifi c position in the list based on an integer index, much like
an array. You can search a list, iterate through its elements, and perform operations on
a range of values in the list. Lists are commonly used because there are many situations
in programming where you need to keep track of a list of objects. For example, suppose
you have a website that sells electronic equipment and you execute a database query that
Search WWH ::




Custom Search