Java Reference
In-Depth Information
type and return an unmodifiable view of the generic type. For example, the following code
creates an unmodifiable List ( list2 ) that stores String objects:
List<String> list1 = new ArrayList<>();
List<String> list2 = Collections.unmodifiableList(list1);
Software Engineering Observation 16.6
You can use an unmodifiable wrapper to create a collection that offers read-only access to
others, while allowing read-write access to yourself. You do this simply by giving others a
reference to the unmodifiable wrapper while retaining for yourself a reference to the
original collection.
public static method headers
<T> Collection<T> unmodifiableCollection(Collection<T> c)
<T> List<T> unmodifiableList(List<T> aList)
<T> Set<T> unmodifiableSet(Set<T> s)
<T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
<K, V> Map<K, V> unmodifiableMap(Map<K, V> m)
<K, V> SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, V> m)
Fig. 16.21 | Unmodifiable wrapper methods.
16.15 Abstract Implementations
The collections framework provides various abstract implementations of Collection in-
terfaces from which you can quickly “flesh out” complete customized implementations.
These abstract implementations include a thin Collection implementation called an Ab-
stractCollection , a List implementation that allows array-like access to its elements
called an AbstractList , a Map implementation called an AbstractMap , a List implemen-
tation that allows sequential access (from beginning to end) to its elements called an
AbstractSequentialList , a Set implementation called an AbstractSet and a Queue im-
plementation called AbstractQueue . You can learn more about these classes at http://
docs.oracle.com/javase/7/docs/api/java/util/package-summary.html . To write a
custom implementation, you can extend the abstract implementation that best meets your
needs, implement each of the class's abstract methods and override the class's concrete
methods as necessary.
16.16 Wrap-Up
This chapter introduced the Java collections framework. You learned the collection hier-
archy and how to use the collections-framework interfaces to program with collections
polymorphically. You used classes ArrayList and LinkedList , which both implement the
List interface. We presented Java's built-in interfaces and classes for manipulating stacks
and queues. You used several predefined methods for manipulating collections. You
learned how to use the Set interface and class HashSet to manipulate an unordered collec-
tion of unique values. We continued our presentation of sets with the SortedSet interface
 
 
 
Search WWH ::




Custom Search