Expected behavior of a Streams.cycled() instance - found issues and questions

Dmitry Bessonov dmitry.bessonov at oracle.com
Tue Oct 23 09:10:26 PDT 2012


  In the current API there is a utility class java.util.streams.Streams
  which contains besides others method cycled().

  There are few problems and questions regarding how a properly cycled 
stream should behave
  in terms of conforming to the standard contract of the Stream interface.

  Some of the questions apply to any infinite stream.

  0) (Sorry if this has been discussed already) - is there any legal way 
to get flags of a random Stream instance?

1) Should it be allowed (as it is now) to concat to the end of an 
infinite (cycled for example) stream?

  2) Getting next element of a flatMap of a cycled stream leads to hanging:
     Streams.cycle(Arrays.asList(1, 2, 3)).flatMap((Block<Object> sink, 
Integer element) -> {}).iterator().next();

  3) cycledStream.noneMatch((obj) -> false)) leads to hanging as well 
though it probably should not.

  4) Getting iterator for a sorted cycled stream leads to 
java.lang.OutOfMemoryError: Java heap space
     Streams.cycle(Arrays.asList(1, 2, 

  5) cycledStream.uniqueElements() leads to IllegalStateException though 
it should not according to common sense.

  6) This will lead to hanging:
     Streams.cycle(Arrays.asList(1, 2, 3)).filter((obj) -> 

  7) This will lead to hanging as well:
     Streams.cycle(Arrays.asList(1, 2, 3)).allMatch((obj) -> true);

  8) And this hangs up also:
     Streams.cycle(Arrays.asList(1, 2, 3)).anyMatch((obj) -> false);


More information about the lambda-dev mailing list