use Stream.toList() or collect(Collectors.toUnmodifiable*()) if returned Collection should be immutable.
Comment From: philwebb
We unfortunately can't do a global search/replace with these since toList() returns an unmodifiable list where as toCollection(ArrayList::new) does not. Looking at the changes I think at least https://github.com/spring-projects/spring-boot/pull/36509/files#diff-0a3440d22ed49ae2bf92694192d1a9de54c92e6c1b8cdcacd2ac7919c5f229bdR61 will fail.
It's certainly work us seeking more of these out, but we'll have to be quite careful about where we do it.
Comment From: quaff
We unfortunately can't do a global search/replace with these since
toList()returns an unmodifiable list where astoCollection(ArrayList::new)does not. Looking at the changes I think at least https://github.com/spring-projects/spring-boot/pull/36509/files#diff-0a3440d22ed49ae2bf92694192d1a9de54c92e6c1b8cdcacd2ac7919c5f229bdR61 will fail.It's certainly work us seeking more of these out, but we'll have to be quite careful about where we do it.
Collector.toList() is using ArrayList::new internally, It will return an mutable list, am I missing something?
Comment From: wilkinsona
That behavior isn't guaranteed:
There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).
As such, we can't rely upon it and need to use the Supplier variant where, for example, we require the result to be mutable.
Comment From: quaff
That behavior isn't guaranteed:
There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).
As such, we can't rely upon it and need to use the Supplier variant where, for example, we require the result to be mutable.
So we should keep mutable List as it is, and others refactor to Collectors.toUnmodifiableList() ensure immutability, WDYT?
Comment From: wilkinsona
In 3.0.x and later branches, Stream.toList() should be used the vast majority of the time. Similarly, in 2.7.x Collectors.toList() should be used the vast majority of the time. On any maintained branch where mutability is required, Collectors.toList(ArrayList::new) should be used.
Comment From: quaff
In 3.0.x and later branches,
Stream.toList()should be used the vast majority of the time. Similarly, in 2.7.xCollectors.toList()should be used the vast majority of the time. On any maintained branch where mutability is required,Collectors.toList(ArrayList::new)should be used.
I've updated this PR as you suggested @wilkinsona