In Java, how can you make an ArrayList
read-only (so that no one can add elements, edit, or delete elements) after initialization?
Pass the ArrayList
into Collections.unmodifiableList()
. It returns an unmodifiable view of the specified list. Only use this returned List
, and never the original ArrayList
.