Java Reference
In-Depth Information
tions.synchronizedMap(new HashMap<String, In-
teger>()); . Query operations on the returned map “read through” to the
its collection views, result in an UnsupportedOperationException .
Note For performance reasons, collections implementations are unsynchron-
ized—unsynchronized collections have better performance than synchronized collec-
as synchronizedSet() .
You might be wondering about the purpose for the various “ empty ” class methods
in the Collections class. For example, static final <T> List<T>
emptyList() returnsanimmutableemptylist,asin List<String> ls = Col-
lections.emptyList(); .Thesemethodsarepresentbecausetheyofferauseful
alternative to returning null (and avoiding potential NullPointerException s) in
certain contexts. Consider Listing 5-26 .
Listing 5-26. Empty and nonempty List s of Bird s
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
class Birds
private List<String> birds;
birds = Collections.emptyList();
Birds(String... birdNames)
birds = new ArrayList<String>();
for (String birdName: birdNames)
Search WWH ::

Custom Search