copyOf method for collections
The copyOf method is introduced to create an unmodifiable copy of collections. For example, suppose you have a list and you need an immutable or unmodifiable copy, you can use the copyOf function. If you have used collections, you may wonder how it is different than Collections.unmodifiableCollection, which promises to do the same thing, that is create an unmodifiable copy of the collection. Well, although both methods give us an unmodifiable copy, when we use copyOf on a collection, say a list, it returns a list that cannot further be modified plus any changes to the original list do not impact the copied list. On the other hand, Collections.unmodifiableCollection does return an unmodifiable list in the aforementioned ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access