Complementing the another answer, there are several differences between these two methods (reference).
One has already been quoted: Arrays.asList
returns a list that can be modified (but with caveats, see below), while List.of
returns a list that nay can be modified:
// esta lista pode ser modificada (posso mudar elementos já existentes)
List<String> asList = Arrays.asList("maça", "laranja");
// mudando o elemento da posição zero
asList.set(0, "abacaxi"); // OK
System.out.println(asList); // [abacaxi, laranja]
// esta lista não pode ser modificada
List<String> listOf = List.of("maça", "laranja");
listOf.set(0, "abacaxi"); // Erro: java.lang.UnsupportedOperationException
However, it is worth remembering one detail: the list returned by Arrays.asList
has fixed size, ie, I can only change the values of existing positions (as I did above with the method set
). But if I want to add or remove elements, it will give error:
List<String> asList = Arrays.asList("maça", "laranja");
// qualquer método que mude o tamanho da lista dá erro
asList.add("banana"); // Erro: java.lang.UnsupportedOperationException
asList.remove(0); // Erro: java.lang.UnsupportedOperationException
Another difference is the treatment given for null values. Arrays.asList
accepts null elements, while List.of
nay:
// ok, pode ter null
List<String> asList = Arrays.asList("maça", "laranja", null);
System.out.println(asList); // [abacaxi, laranja, null]
// erro, não pode ter null
List<String> listOf = List.of("maça", "laranja", null); // NullPointerException
The same occurs when I check for a null element:
List<String> asList = Arrays.asList("maça", "laranja");
// pode verificar se contém elemento nulo
System.out.println(asList.contains(null)); // false
List<String> listOf = List.of("maça", "laranja");
// não pode verificar se contém elemento nulo
System.out.println(listOf.contains(null)); // NullPointerException
Another difference is that when we pass an array to these methods, Arrays.asList
keeps a reference to the original array, and therefore changes in one reflect on the other. Already with List.of
this does not happen, and changes in one do not reflect in the other:
String[] frutas = { "maça", "laranja" };
List<String> asList = Arrays.asList(frutas);
System.out.println(asList); // [maça, laranja]
// modificações no array refletem na lista
frutas[0] = "abacaxi";
System.out.println(asList); // [abacaxi, laranja]
// modificações na lista refletem no array
asList.set(1, "banana");
System.out.println(frutas[1]); // banana
//-------------------------------------------
String[] frutas2 = { "maça", "laranja" };
List<String> listOf = List.of(frutas2);
System.out.println(listOf); // [maça, laranja]
// modificações no array NÃO refletem na lista
frutas2[0] = "abacaxi";
System.out.println(listOf); // [maça, laranja]
Finally, it is also worth mentioning Collections.unmodifiableList
, that as well as List.of
, returns a list that cannot be modified (set
, add
and remove
launch UnsupportedOperationException
), but the difference is that unmodifiableList
accepts null values.
According to the documentation, the first returns an immutable list, and the second returns only a changeable converted list. I just don’t know how far that list goes.
– user28595
Now I read it better, Lisf.of returns a list that cannot have its structure modified, IE, is read-only, you can not add, remove or replace any of its items. Arrays.asList returns a fixed-size list based on the number of elements of the corresponding array, but this resulting list is not immutable, you can normally change its structure.
– user28595