Java Reference
In-Depth Information
Line 72 calls Properties method load to restore the contents of the Properties
object from the InputStream specified as the first argument (in this case, a FileInput-
Stream ). Line 86 calls Properties method keySet to obtain a Set of the property names.
Because class Properties stores its contents as Object s, a Set of Object references is
returned. Line 91 obtains the value of a property by passing a key to method getProperty .
16.13 Synchronized Collections
In Chapter 23, we discuss multithreading . Except for Vector and Hashtable , the collec-
tions in the collections framework are unsynchronized by default, so they can operate effi-
ciently when multithreading is not required. Because they're unsynchronized, however,
concurrent access to a Collection by multiple threads could cause indeterminate results
or fatal errors—as we demonstrate in Chapter 23. To prevent potential threading prob-
lems, synchronization wrappers are used for collections that might be accessed by multi-
ple threads. A wrapper object receives method calls, adds thread synchronization (to
prevent concurrent access to the collection) and delegates the calls to the wrapped collec-
tion object. The Collections API provides a set of static methods for wrapping collec-
tions as synchronized versions. Method headers for the synchronization wrappers are listed
in Fig. 16.20. Details about these methods are available at http://docs.oracle.com/
javase/7/docs/api/java/util/Collections.html . All these methods take a generic
type and return a synchronized view of the generic type. For example, the following code
creates a synchronized List ( list2 ) that stores String objects:
List<String> list1 = new ArrayList<>();
List<String> list2 = Collections.synchronizedList(list1);
public static method headers
<T> Collection<T> synchronizedCollection(Collection<T> c)
<T> List<T> synchronizedList(List<T> aList)
<T> Set<T> synchronizedSet(Set<T> s)
<T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
<K, V> Map<K, V> synchronizedMap(Map<K, V> m)
<K, V> SortedMap<K, V> synchronizedSortedMap(SortedMap<K, V> m)
Fig. 16.20 | Synchronization wrapper methods.
16.14 Unmodifiable Collections
The Collections class provides a set of static methods that create unmodifiable wrap-
pers for collections. Unmodifiable wrappers throw UnsupportedOperationException s if
attempts are made to modify the collection. In an unmodifiable collection, the references
stored in the collection are not modifiable, but the objects they refer are modifiable unless
they belong to an immutable class like String . Headers for these methods are listed in
Fig. 16.21. Details about these methods are available at http://docs.oracle.com/
javase/7/docs/api/java/util/Collections.html . All these methods take a generic
 
 
 
Search WWH ::




Custom Search