Search code examples
javacollectionsjakarta-eearraylist

make ArrayList Read only


In Java, how can you make an ArrayList read-only (so that no one can add elements, edit, or delete elements) after initialization?


Solution

  • 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.