Module 01
Module 01
• Here, E specifies the type of objects that the collection will hold.
• Collection extends the Iterable interface. This means that all collections can be cycled
through by use of the foreach style for loop.
• Collection declares the core methods that all collections will have. These
methods are summarized in Table 20-1.
The Collection
Interfaces
The Collection Interfaces
• In addition to the methods defined by Collection, List defines some of its own,
which are summarized in Table 20-2.
The Collection Interfaces
• several of these methods will throw an UnsupportedOperationException if the list cannot be
modified,
• a ClassCastException is generated when one object is incompatible with another, such as when an
attempt is made to add an incompatible object to a list.
• Also, several methods will throw an IndexOutOfBoundsException if an invalid index is used.
• A NullPointerException is thrown if an attempt is made to store a null object and null elements are
not allowed in the list.
• An IllegalArgumentException is thrown if an invalid argument is used.
• List includes the of( ) factory method, which has a number of overloads.
• Each version returns an unmodifiable, value-based collection that is comprised of the arguments that
it is passed.
• The primary purpose of of( ) is to provide a convenient, efficient way to create a small List collection.
• There are 12 overloads of of( ).
• One takes no arguments and creates an empty list.
• It is shown here: static <E> List<E> of( )
The Collection Interfaces
The Collection Interfaces
• The ArrayList class extends AbstractList and implements the List interface.
• ArrayList is a generic class that has this declaration:
class ArrayList<E>
• Here, E specifies the type of objects that the list will hold.
• But, sometimes, you may not know until run time precisely how large an array you
need.
• To handle this situation, the Collections Framework defines ArrayList.
• In essence, an ArrayList is a variable-length array of object references.
• That is, an ArrayList can dynamically increase or decrease in size.
• Array lists are created with an initial size.
• When this size is exceeded, the collection is automatically enlarged.
• When objects are removed, the array can be shrunk.
The Collection Classes
The ArrayList Class
• LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were
inserted.
• This allows insertion-order iteration over the set.
• That is, when cycling through a LinkedHashSet using an iterator, the elements will be returned in the order in which they
were inserted.
•
• To see the effect of LinkedHashSet, try substituting LinkedHashSet for HashSet in the preceding
program.
• The first form constructs an empty tree set that will be sorted in ascending order according to the
natural order of its elements.
• The second form builds a tree set that contains the elements of c.
• The third form constructs an empty tree set that will be sorted according to the comparator specified
by comp.
• The fourth form builds a tree set that contains the elements of ss.
The Collection Classes
The TreeSet Class
Because TreeSet implements the NavigableSet
interface, you can use the methods defined by
NavigableSet to retrieve elements of a TreeSet.
For example, assuming the preceding program, the
following statement uses subSet( ) to obtain a subset
of ts that contains the elements between C (inclusive)
and F (exclusive).
It then displays the resulting set.
System.out.println(ts.subSet("C", "F"));
The output from this statement is shown here:
[C, D, E]
• The first constructor builds an empty queue. Its starting capacity is 11.
• The second constructor builds a queue that has the specified initial capacity.
• The third constructor specifies a comparator,
• The fourth builds a queue with the specified capacity and comparator.
• The last three constructors create queues that are initialized with the elements of the collection
passed in c.
• In all cases, the capacity grows automatically as elements are added.
The Collection Classes
The PriorityQueue Class
• If no comparator is specified when a PriorityQueue is constructed, then the default comparator for
the type of data stored in the queue is used.
• The default comparator will order the queue in ascending order.
• Thus, the head of the queue will be the smallest value.
• However, by providing a custom comparator, you can specify a different ordering scheme.
• For example, when storing items that include a time stamp, you could prioritize the queue such that
the oldest items are first in the queue.
• You can obtain a reference to the comparator used by a PriorityQueue by calling its comparator( )
method, shown here:
Comparator<? super E> comparator( )
• It returns the comparator.
• If natural ordering is used for the invoking queue, null is returned.
The Collection Classes
The ArrayDeque Class
• The ArrayDeque class extends AbstractCollection and implements the Deque interface.
• It adds no methods of its own.
• ArrayDeque creates a dynamic array and has no capacity restrictions.
• ArrayDeque is a generic class that has this declaration:
class ArrayDeque<E>
• Here, E specifies the type of objects stored in the collection.
• To use an iterator to cycle through the contents of a collection, follow these steps:
1. Obtain an iterator to the start of the collection by calling the collection’s iterator(
) method.
2. Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as
hasNext( ) returns true.