RFR: 8372946 - TreeMap sub-map entry spliterator is expensive [v4]

Chen Liang liach at openjdk.org
Mon Feb 23 15:35:41 UTC 2026


On Mon, 23 Feb 2026 15:30:05 GMT, Chen Liang <liach at openjdk.org> wrote:

>> It ends up something like this:
>> 
>> 
>> public static void main(String[] args) {
>>     List<String> strings = new LinkedList<>();
>>     strings.add("foo");
>>     strings.add("bar");
>> 
>>     Spliterator<String> s = Spliterators.spliterator(strings, Spliterator.DISTINCT); // Don't create iterator yet
>>     strings.remove(strings.iterator().next());
>>     s.forEachRemaining(System.out::println); // Spliterator creates iterator here, after the .remove call. No CME
>> 
>>     s = Spliterators.spliteratorUnknownSize(strings.iterator(), Spliterator.DISTINCT); // Eagerly create the iterator
>>     strings.remove(strings.iterator().next()); // Modifying after the iterator was created
>>     s.forEachRemaining(System.out::println); // ConcurrentModificationException
>> }
>> 
>> 
>> So it's just a side effect of `spliteratorUnknownSize` needing the iterator to be created already. I think the test skip is valid, then - it's true that this case is no longer lazy like it was.
>
> Yes, seems you cannot create a spliterator with a lazy iterator. This is fine.

Alternatively we can override `stream()` to call the `StreamSupport.stream` that takes a `Supplier<? extends Spliterator<T>>` instead.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/28608#discussion_r2841514718


More information about the core-libs-dev mailing list