amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 249 forks source link

Make toList() immutable to align with JDK 16 toList() #244

Closed amaembo closed 2 years ago

amaembo commented 3 years ago

We have StreamEx.toList() that explicitly specified as returning a mutable collection. However, in JDK 16, Stream.toList() method was added that is explicitly specified as returning an unmodifiable collection. So, since JDK 16, StreamEx violates the Stream interface specification. We need to introduce a breaking change to align them:

The same will be done for toSet(). For now, let's keep toMap() as is, as there are too many overloads and changes there would make things even more confusing, and bloated.

Also, the behavior of toListAndThen() and toSetAndThen() is not changed: the collection supplied to the lambda will still be mutable (even though it's not specified).

msladek commented 2 years ago

Breaking the contract of toList/Set/Map in a single version change could cause major trouble for clients. Please provide a proper migration path:

  1. First introduce toMutableList/Set/Map and hint in the Javadoc of toList/Set/Map that toMutableList/Set/Map should be used instead. Maybe even a deprecation annotation is in order.
  2. For the next major version toList/Set/Map can be redirected to toImmutableList/Set/Map and the Javadoc hint / deprecation annotation can be removed again.
amaembo commented 2 years ago

@msladek thank you for the feedback. I updated the issue description according to the current plan.