groovy - Implementing iterator interface with map doesn't work with java 8 streams -
i'll start code:
import java.util.stream.stream import java.util.stream.streamsupport def hasmore1 = true def iter1 = new iterator<integer>() { boolean hasnext() { def retval = hasmore1; hasmore1 = false; retval } integer next() { 42 } } def hasmore2 = true def iter2 = [ hasnext: { -> def retval = hasmore2; hasmore2 = false; retval }, next: { -> 42 } ] iterator<integer> def stream1 = streamsupport.stream(spliterators.spliterator(iter1, 1, 0), false) def stream2 = streamsupport.stream(spliterators.spliterator(iter2, 1, 0), false) stream1.foreach { println } // prints 42 stream2.foreach { println } // throws java.lang.unsupportedoperationexception
these 2 methods of implementing iterator in groovy seem semantically equivalent, , in normal use cases, iterating foreach, , use in list comprehensions seem treat them same. java 8 streams api seems treat them different demonstrated above. how , why? there can map-implementing interface version make behave correctly?
seems has been fixed in groovy 2.3.8
it may have been similar bug fixed in same release: https://issues.apache.org/jira/browse/groovy-7104
Comments
Post a Comment