Skip to main content
added 245 characters in body
Source Link
import static java.util.stream.Collectors.toList;*; ... List<String> strings = Stream.of("foo", "bar", "baz").toList(); // Java 16, immutable List<String> strings = Stream.of("foo", "bar", "baz").collect(toUnmodifiableList()); // Java 10, immutable List<String> strings = Stream.of("foo", "bar", "baz").collect(toList()); // Java 8, an ArrayList in practice 

But don't expect these lists to be mutable: Collectors.toList() is documented to have doesn't guarantee"no guarantees on the type, mutability, serializability, or thread-safety" but in practice it, and has always returned an Stream.toList()ArrayList will return unmodifiable list for sure, and that's very unlikely to change because it would break a lot of code.

import static java.util.stream.Collectors.toList; ... List<String> strings = Stream.of("foo", "bar", "baz").toList(); // Java 16 List<String> strings = Stream.of("foo", "bar", "baz").collect(toList()); // Java 8 

But don't expect these lists to be mutable: Collectors.toList() doesn't guarantee it, and Stream.toList() will return unmodifiable list for sure.

import static java.util.stream.Collectors.*; ... List<String> strings = Stream.of("foo", "bar", "baz").toList(); // Java 16, immutable List<String> strings = Stream.of("foo", "bar", "baz").collect(toUnmodifiableList()); // Java 10, immutable List<String> strings = Stream.of("foo", "bar", "baz").collect(toList()); // Java 8, an ArrayList in practice 

Collectors.toList() is documented to have "no guarantees on the type, mutability, serializability, or thread-safety" but in practice it has always returned an ArrayList, and that's very unlikely to change because it would break a lot of code.

added 21 characters in body
Source Link

Arrays.asList(...) will give you a List* backed by anthe passed in arguments array, so it cannot change length.
But you can call List.set(...), so it's still mutable.

Arrays.asList(...) will give you a List* backed by an array, so it cannot change length.
But you can call List.set(...), so it's still mutable.

Arrays.asList(...) will give you a List* backed by the passed in arguments array, so it cannot change length.
But you can call List.set(...), so it's still mutable.

clarify behaviour of the lists
Source Link
Ilya Serbis
  • 22.5k
  • 8
  • 92
  • 78
import static java.util.stream.Collectors.toList; ... varList<String> strings = Stream.of("foo", "bar", "baz").toList(); // Java 16 List<String> strings = Stream.of("foo", "bar", "baz").collect(toList()); // Java 8 

But preferablydon't expect these lists to be mutable: Collectors.toList() doesn't guarantee it, and Stream.toList() will return unmodifiable list for sure.

Preferably, just use the Stream without collecting it to a List.

import static java.util.stream.Collectors.toList; ... var strings = Stream.of("foo", "bar", "baz").toList(); // Java 16 List<String> strings = Stream.of("foo", "bar", "baz").collect(toList()); // Java 8 

But preferably, just use the Stream without collecting it to a List.

import static java.util.stream.Collectors.toList; ... List<String> strings = Stream.of("foo", "bar", "baz").toList(); // Java 16 List<String> strings = Stream.of("foo", "bar", "baz").collect(toList()); // Java 8 

But don't expect these lists to be mutable: Collectors.toList() doesn't guarantee it, and Stream.toList() will return unmodifiable list for sure.

Preferably, just use the Stream without collecting it to a List.

added 3 characters in body
Source Link
Loading
deleted 242 characters in body
Source Link
Loading
added 240 characters in body
Source Link
Loading
added 255 characters in body
Source Link
Loading
added 76 characters in body
Source Link
Loading
deleted 2 characters in body
Source Link
Loading
deleted 127 characters in body
Source Link
Loading
added 15 characters in body
Source Link
Loading
deleted 16 characters in body
Source Link
Loading
deleted 16 characters in body
Source Link
Loading
added 240 characters in body
Source Link
Loading
added 55 characters in body
Source Link
Loading
Commonmark migration
Source Link
Loading
added 13 characters in body
Source Link
Loading
added 64 characters in body
Source Link
Loading
deleted 9 characters in body
Source Link
Loading
added 9 characters in body
Source Link
Loading
added 109 characters in body
Source Link
Loading
added 11 characters in body
Source Link
Faraz
  • 6.3k
  • 6
  • 44
  • 99
Loading
added 15 characters in body
Source Link
Loading
added 15 characters in body
Source Link
Loading
added 150 characters in body
Source Link
Loading