On Thu, 3 Sep 2026 13:42:06 GMT, Viktor Klang <[email protected]> wrote:

>> ## Summary
>> 
>> [`JDK-8223933`](https://bugs.openjdk.org/browse/JDK-8223933)
>> 
>> `Stream.distinct()` must remove duplicates according to `Object.equals`. On 
>> sorted streams it only compared each element to the previous one, which 
>> fails when `compareTo` is inconsistent with `equals`, so duplicates can 
>> still be emitted.
>> 
>> ## Fix
>> Keep the sorted-path consecutive check, and also track emitted elements in a 
>> `HashSet` so uniqueness always follows `equals`. Add a regression test for 
>> the reported case and for equals-duplicates in different sort groups.
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Just weighing in: I suspect that this isn't really solvable without risking 
> breakage. If `sorted()` is to also enforce that `prev.compareTo(next)` is 
> aligned with the result of `prev.equals(next)` then usages that are currently 
> working will cease to work: 
> 
> 
> jshell> (new BigDecimal("0.0")).compareTo(new BigDecimal("0.00"))
> $1 ==> 0
> 
> jshell> (new BigDecimal("0.0")).equals(new BigDecimal("0.00"))
> $2 ==> false
> 
> 
> If instead a distinct() following a sorted() always re-processes the entire 
> Stream, then it would risk changing the output of existing Stream usages 
> (silently). 🤔

@viktorklang-ora right. Honestly, I would leave it as is.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/32670#issuecomment-5527123361

Reply via email to