Java Reference
In-Depth Information
be to return the attributes as an unmodifiable collection. Here's how At-
tributedImpl could implement this:
public Collection<Attr> attrs() {
return Collections.
unmodifiableCollection(attrTable.values());
}
Generally, iterators must be used immediately after they are obtained
from the iterator method, with no intervening changes to the collec-
tionotherwise, use of the iterator will encounter a ConcurrentModifica-
tionException . In contrast, you can ask for an unmodifiable collection,
and then use it at some later time when the original collection has un-
dergone arbitrary changes. Exposing information via a collection also al-
lows the users of your class to utilize all the Collection utility methods.
For example, if you expose your information as an unmodifiable list, it
can be searched and sorted without your having to define search and
sort methods.
21.10.3. The Checked Wrappers
A List<String> is guaranteed at compile time to only ever hold String ob-
jectsunless you have to pass it to legacy code as a raw type, in which
case all guarantees are off (and you will get an "unchecked" warning).
Dealing with legacy code that is unaware of generic types is a practical
necessity. However, tracking down problems can be difficult. If you pass
a List<String> to a legacy method that erroneously inserts a Number , you
won't discover the problem until another part of your code tries to force
the Number to be a String . The type-safe checked wrappers have been
provided to help you with this problem. A checked wrapper will make a
runtime check to enforce the type safety lost when the collection is used
as a raw type. This allows errors to be detected as soon as they occur-
when that erroneous Number is inserted.
 
Search WWH ::




Custom Search