It might be worth exploring this. I'll note that gatherers (JDK 24) provide a hook for adding such functionality in Java. Gatherers4j has withIndex (though we'd likely implement it differently):
https://tginsberg.github.io/gatherers4j/gatherers/sequence-operations/withindex/ As well as a bunch of other "index" operations. Paul. On Fri, Mar 21, 2025 at 2:00 AM Per Nyfelt <per.nyf...@nordnet.se> wrote: > > Hi , > > > > I suggest that the withIndex method in DefaultGroovyMethods is overloaded > with an option to support streams as well > > > > Given > > > > names = ['per', 'karin', 'tage', 'arne', 'sixten', 'ulrik'] > > > > I can find the 4:th element with > > println names[3] > > > > or if I only have an iterator with > > println names.iterator().withIndex().find { it, idx -> 3 == idx }[0] > > > > For a stream I can do it by using a java.util.concurrent.atomic.AtomicInteger: > > AtomicInteger index = new AtomicInteger() > > println names.stream().find(n -> 3 == index.getAndIncrement()) > > > > I have seen that libraries more and more often will expose a stream api > rather than a Collection or an Iterator so it would be very nice if I could > just do > > println names.stream().withIndex().find { it, idx -> 3 == idx}[0] > > > > or alternatively add a findWithIndex so that this would be possible: > > println names.stream().findWithIndex { it, idx -> 3 == idx} > > > > What do you think? > > > > Regards, > > Per