Java Reference
In-Depth Information
snapshot but don't have such a guarantee, you can create a snapshot
by making a simple copy of the collection, such as with an ArrayList :
public <T> Iterator<T>
snapshotIterator(Collection<? extends T> coll) {
return new ArrayList<T>(coll).iterator();
}
Any utility method that returns a collection will invariably be a generic
method. We introduce the type variable T to represent the unknown type
of the collection being passed in. The snapshotIterator method can ac-
cept any collection of T or a subtype of T , but only guarantees that it
returns an Iterator<T> . [2]
[2] It is possible to return Iterator<? super T> , which is slightly more general than Iterator<T>, but
to do so would mean you wouldn't be able to assign the result to a variable of type Iterator<T> . That
would be a very annoying restriction.
Many of the iterators defined in the java.util package are what is known
as fail-fast iterators. Such iterators detect when a collection has been
modified other than through the iterator itself, and fail quickly and
cleanly by throwing an exceptiona ConcurrentModificationException rather
than risk performing an action whose behavior may be unsafe.
 
 
Search WWH ::




Custom Search