Vineeth Bhaskaran Vineeth Bhaskaran - 1 month ago 10
Java Question

Is java util List is mutable or Not?

We are able to

add
/
remove
elements to
List
using
add()
/
remove()
methods without creating another list which looks similar to
StringBuffer
append()
. So i think
List
is mutable. Can anyone confirm my understanding is correct? If it is wrong please explain with below code

List<String> strList = new ArrayList<String>();
strList.add("abc");
strList.add("xyz");

Answer

Yes, the java.util.List is mutable and it is not creating another List instance on add() or remove().

If you are looking for immutable list - check Guava implementation of ImmutableList or Collections.unmodifiableList which throws java.lang.UnsupportedOperationException on modifications.