Chapter 20
data structure
A data structure is a collection of data organized in some fashion. The structure not only stores data but also supports operations for accessing and manipulating the data.
List
A list stores an ordered collection of elements.
Term 1
All the concrete classes except PriorityQueue in the Java Collections Framework implement the Cloneable and Serializable interfaces. Thus, their instances can be cloned and serialized.
Term 4
Comparator can be used to compare the objects of a class that doesn't implement Comparable.
Iterable
Each collection is Iterable. You can obtain its Iterator object to traverse all the elements in the collection. Iterator is a classic design pattern for walking through a data structure without having to expose the details of how data is stored in the data structure. The Collection interface extends the Iterable interface. The Iterable interface defines the iterator method, which returns an iterator. The Iterator interface provides a uniform way for traversing elements in various types of collections. The iterator() method in the Iterable interface returns an instance of Iterator, as shown in Figure 20.2, which provides sequential access to the elements in the collection using the next() method. You can also use the hasNext() method to check whether there are more elements in the iterator, and the remove() method to remove the last element returned by the iterator.
priority queue
In a priority queue, the element with the highest priority is removed first. A queue is a first-in, first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned priorities. When accessing elements, the element with the highest priority is removed first. This section introduces queues and priority queues in the Java API.
linked list
LinkedList stores elements in a linked list. Which of the two classes you use depends on your specific needs. If you need to support random access through an index without inserting or removing elements at the beginning of the list, ArrayList offers the most efficient collection. If, however, your application requires the insertion or deletion of elements at the beginning of the list, you should choose LinkedList. A list can grow or shrink dynamically. Once it is created, an array is fixed. If your application does not require the insertion or deletion of elements, an array is the most efficient data structure.
Lists
Lists store an ordered collection of elements.
Queue
Queues store objects that are processed in a first-in, first-out fashion.
Stack
Stacks store objects that are processed in a last-in, first-out fashion.
Collection interface
The Collection interface defines the common operations for lists, vectors, stacks, queues, priority queues, and sets.
collection
The Collection interface defines the common operations for lists, vectors, stacks, queues, priority queues, and sets. The Java Collections Framework supports two types of containers: One for storing a collection of elements is simply called a collection.
The Collections class
The Collections class contains static methods to perform common operations in a collection and a list. Section 11.12 introduced several static methods in the Collections class for array lists. The Collections class contains the sort, binarySearch, reverse, shuffle, copy, and fill methods for lists, and max, min, disjoint, and frequency methods for collections, as shown in Figure 20.7.
Java Collections Framework
The Java Collections Framework supports sets, lists, queues, and maps. They are defined in the interfaces Set, List, Queue, and Map.
List interface
The List interface extends the Collection interface and defines a collection for storing elements in a sequential order. To create a list, use one of its two concrete classes: ArrayList or LinkedList.
Term 6
The Queue interface represents a queue. The PriorityQueue class implements Queue for a priority queue.
Term 5
TheVectorclassextendstheAbstractListclass.StartingwithJava2,Vectorhas been the same as ArrayList, except that the methods for accessing and modifying the vector are synchronized. The Stack class extends the Vector class and provides several methods for manipulating the stack.
Term 2
To allow duplicate elements to be stored in a collection, you need to use a list. A list not only can store duplicate elements but also allows the user to specify where they are stored. The user can access elements by an index.
Term 3
Two types of lists are supported: ArrayList and LinkedList. ArrayList is a resizable-array implementation of the List interface. All the methods in ArrayList are defined in List. LinkedList is a linked-list implementation of the List interface. In addition to implementing the List interface, this class provides the methods for retrieving, inserting, and removing elements from both ends of the list.
Vector
Vector is a subclass of AbstractList, and Stack is a subclass of Vector in the java API. The Java Collections Framework was introduced in Java 2. Several data structures were sup- ported earlier, among them the Vector and Stack classes. These classes were redesigned to fit into the Java Collections Framework, but all their old-style methods are retained for compatibility. Vector is the same as ArrayList, except that it contains synchronized methods for accessing and modifying the vector. Synchronized methods can prevent data corruption when a vector is accessed and modified by two or more threads concurrently. We will dis- cuss synchronization in Chapter 30, Multithreading and Parallel Programming. For the many applications that do not require synchronization, using ArrayList is more efficient than using Vector. The Vector class extends the AbstractList class. It also has the methods contained in the original Vector class defined prior to Java 2, as shown in Figure 20.10.
comparator
What if the elements' classes do not implement the Comparable interface? Can these elements be compared? You can define a comparator to compare the elements of different classes. To do so, define a class that implements the java.util.Comparator<T> interface and overrides its compare method.
convenience abstract classes
a concrete class that extends the abstract class rather implements all the methods in the interface. The abstract classes such as AbstractCollection are provided for convenience. For this reason, they are called convenience abstract classes.