Java Reference
In-Depth Information
11.1 Lists
The ArrayList class from Chapter 10 has several advantages over an array: It keeps
track of its own size for you, it allows you to insert and remove data at arbitrary
places in the array, and it resizes itself for you if it gets full.
In this section we'll learn about an object called a LinkedList , which is similar
to an ArrayList . We'll also look at generalizing collections and discuss a useful
object called an iterator that lets you examine the elements of any collection.
Collections
In Chapters 7 and 8 we discussed ways to use arrays and classes to store data. The
notion of organizing and structuring data is an important one that helps us solve com-
plex problems. Entities that store and manage data are also called data structures.
Data structures can be used to implement sophisticated data storage objects called
collections.
Collection
An object that stores a group of other objects, called its elements.
An ArrayList is an example of a collection. A collection uses a data structure
internally to store its elements, such as an array or a set of objects that refer to one
another. For example, an ArrayList is implemented using an array as its data struc-
ture, and a TreeSet (a collection introduced later in this chapter) is implemented
using a data structure called a binary search tree.
Collections are categorized by the types of elements they store, the operations they
allow you to perform on those elements, and the speed or efficiency of those opera-
tions. Here are some examples of collections:
List: An ordered collection of elements, often accessed by integer indexes or by
iteration.
Stack: A collection in which the last element added is the first one to be
removed.
Queue: A collection in which elements are removed in the same order in which
they were added.
Set: A collection of elements that is guaranteed to contain no duplicates.
Map: A collection of key/value pairs in which each key is associated with a cor-
responding value.
Java provides a large group of useful collections that allow you to store, access,
search, sort, and manipulate data in a variety of ways. Together, these collections and
classes are known as the Java Collections Framework. This framework is largely
contained in the package java.util .
 
 
Search WWH ::




Custom Search