16.12 Unmodifiable Collections
The Collections
class provides a set of static
methods that create unmodifiable wrappers 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 some of these methods are listed in Fig. 16.19. Details about these methods are available at http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html
. All these methods take a generic type and return an unmodifiable view of the generic type. For example, the following code creates an unmodifiable ...
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.