After much searching, I just found that the only operation that is supported by Spliterator is to READ elements from a Collection.
Can someone tell me about another operation in CRUD that can be supported by Spliterator. I tried to modify elements in a Collection using a Spliterator but it didn't work:
Set<Integer> set = new TreeSet<>();
set.add(2);
set.add(3);
set.add(5);
set.add(6);
Spliterator<Integer> si = set.spliterator();
Spliterator<Integer> sa = si.trySplit();
while(sa.tryAdvance(e -> e= ++e));
System.out.println("original iterator");
while(si.tryAdvance(e-> e = ++e));
System.out.println(set.toString());
Spliteratorcannot modify an underlyingCollection, mainly becauseSpliterator(unlikeIterator) is not a strictlyCollection-bound interface.The definition of
Iterator(fromIterator's JavaDoc):The definition of
Spliterator(fromSpliterator's JavaDoc):EDIT: I just read the code you posted. In this code, you try to mutate an immutable
Integerinstance inside aSpliteratorcall. In this case, neitherIteratornorSpliteratorcan work, because the elements are immutable.Use a
Stream(or anIntStream) for this, together with amap()followed bycollect().