Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java.util.List mutable?

Tags:

java

We are able to add/remove elements to List using add()/remove() methods without creating another list which looks similar to StringBuilder append. Because of this I think List is mutable.

Can anyone confirm my understanding is correct?

If it is wrong please explain the code below:

List<String> strList = new ArrayList<String>();
strList.add("abc");
strList.add("xyz"); 
like image 603
Vineeth Bhaskaran Avatar asked Nov 05 '16 13:11

Vineeth Bhaskaran


People also ask

Are lists in Java immutable?

The List, Set, or Map returned by the of() static factory method is structurally immutable, which means you cannot add, remove, or change elements once added. Calling any mutator method will always cause an UnsupportedOperationException to be thrown.

Is ArrayList immutable in Java?

No, you cannot make the elements of an array immutable. But the unmodifiableList() method of the java. util. Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object.

Are lists mutable or immutable?

The list is a data type that is mutable. Once a list has been created: Elements can be modified. Individual values can be replaced.

What is mutable and immutable list in Java?

A mutable object can be changed after it's created, and an immutable object can't. In Java, everything (except for strings) is mutable by default: public class IntegerPair { int x; int y; IntegerPair(int x, int y) { this.


1 Answers

List mutability

Since List is an interface, the only promise it makes is: "these are the methods you will get".

The List interface describes a mutable List. Notice that it has functions such as add(), set() and remove(). But notice also that these mutators are designated as "optional" operations.

There exist implementations of the List interface which are, in practice, immutable.

List<Integer> mutable = new ArrayList<>();
mutable.add(1);

List<Integer> immutable = Collections.unmodifiableList(mutable);
// try to modify the list
immutable.add(2);
// Exception in thread "main" java.lang.UnsupportedOperationException

Collections.unmodifiableList() returns an instance of the List implementation Collections.UnmodifiableList.

Collections.UnmodifiableList forwards all calls of immutable List functions, correctly to the underlying List. Whereas it implements all mutable List functions by throwing java.lang.UnsupportedOperationException.

List element mutability

If we do:

List<Date> mutable = new ArrayList<>();
mutable.add(new Date());
List<Date> immutable = Collections.unmodifiableList(mutable);
Date myCoolDate = immutable.get(0);

Can we mutate myCoolDate? Absolutely.

myCoolDate.setTime(99999); // works! mutates the original Date object in the `List`.

The List — whether immutable or otherwise — stores copies of references to objects. Once we have a reference to the object (Date myCoolDate = immutable.get(0);), we can mutate the object freely.

The items in an immutable list have no immutability guarantee. They are exactly as mutable as they have always been.

like image 195
Birchlabs Avatar answered Nov 02 '22 07:11

Birchlabs