11

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"); 
Lii
  • 11,553
  • 8
  • 64
  • 88
Vineeth Bhaskaran
  • 2,161
  • 1
  • 29
  • 36

3 Answers3

30

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.

Deva44
  • 93
  • 2
  • 9
Birchlabs
  • 7,437
  • 5
  • 35
  • 54
  • Also note that in the first example you can still add elements like `mutable.add(3);` after creating the `immutable` variable, and those will show up in the `immutable` list as well. So even the `immutable` list can change, just not through the methods of the `immutable` variable. – Jonas Berlin Aug 22 '23 at 12:46
13

java.util.List is an interface. Implementation could be mutable or immutable depending on which you use. If you're wondering about java.util.ArrayList - it 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.

yyunikov
  • 5,719
  • 2
  • 43
  • 78
5

Yes. It's mutable. If you want to have an immutable one, you can wrap it using java utility Collections class: java.util.Collections#unmodifiableList

like this:

  List<Object> mutableList = new ArrayList<>();
  List<Object> immutableList = Collections.unmodifiableList(mutableList);
dvelopp
  • 4,095
  • 3
  • 31
  • 57
  • 2
    I find this answer confusing on the border of misleading. The question, as I understood it, was whether `List` is mutable. You show an example where it is and one where it it not. The examples are correct, so I certainly would not say “Yes. It's mutable”. – Ole V.V. Nov 05 '16 at 13:43
  • Misleading it is. List is neither mutable not immutable, it is an interface whose behaviour is implementation-specific, as clearly detailed in @Birchlabs' answer. – Alain BECKER Dec 19 '22 at 14:25