Java Reference
In-Depth Information
}
The generic LinkedList<> class type now implements the generic Iterable<> interface type, and they
share a common type parameter. The type argument that you supply for LinkedList<> also applies to Iter-
able<> . You can see why the Iterable<> and Iterator<> interfaces need to be generic types. The type
parameter allows them to be automatically adapted to work with objects of any type. Because these inter-
faces are defined as generic types, you can define classes that contain sets of objects of any type and provide
the capability for iterating over the contents using the collection-based for loop simply by implementing
the Iterable<> interface. The interface is automatically customized to work with whatever type of object a
particular container contains.
The Iterator<T> type is a generic interface type that is defined in the java.util package. Your imple-
mentation of the iterator() method must return an object of a class type that implements the Iterator<T>
interface. This is easy to do. The methods in the Iterator<T> interface provide a mechanism for iterating
once over each of the elements of type T in a collection in turn and may also provide the ability to remove
elements. These methods are the following:
T next() returns a reference of type T to the next object that is available from the iterator and
throws an exception of type java.util.NoSuchElementException if no further elements are
available. T is the type parameter for the generic interface and corresponds to the type of objects
stored in the container. Note that you'll also see E used as the type parameter that represents Ele-
ment instead of T for Type .
boolean hasNext() returns true if at least one more element is available from the iterator. When
this is the case, calling the next() method for the iterator returns a reference to the next object in
the container. The method returns false if no more elements are available from the iterator. Thus
this method provides a way for you to check whether calling the next() method for the iterator
returns a reference or throws an exception.
void remove() is an optional operation that removes the last element that was retrieved by the
next() method from the collection. If the remove operation is not supported, this method must
be implemented to throw an exception of type java.lang.UnsupportedOperationException . It
must also throw an exception of type java.lang.IllegalStateException if the next() method
has not been called for the iterator object prior to calling the remove() method.
The first two methods provide a mechanism for iterating over all the elements in a collection such as a
generic linked list. Suppose that you have a LinkedList<String> reference stored in a variable, strings ,
where LinkedList<> is the version that implements Iterable<> . You could use the iterator like this:
Iterator<String> iter = strings.iterator(); // Get an iterator
String str = null; // Stores an element from the list
while(iter.hasNext()) { // If there are more elements
str = iter.next(); // Get the next one...
// Do something with str...
}
The while loop continues to retrieve elements from iter as long as the hasNext() method returns true .
Within the loop, successive elements are retrieved by calling the next() method. You don't need to put
this loop in a try block because the exception that the next() method can throw is of a type derived from
RuntimeException . Removing an element would just involve calling the remove() method for strings
after a call of the next() method, typically after you have analyzed the object retrieved to determine that
Search WWH ::




Custom Search