Making a list unmodifiable

suggest change

The Collections class provides a view that rejects modification:

List<String> ls = new ArrayList<String>();
List<String> unmodifiableList = Collections.unmodifiableList(ls);

Later changes to ls show through this view. Writes through unmodifiableList throw UnsupportedOperationException.

If you want an unmodifiable list with one item you can use:

List<String> unmodifiableList = Collections.singletonList("Only string in the list");

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents