Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8267939

Clarify the specification of iterator and spliterator forEachRemaining

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Build:
      b24
    • Resolved In Build:
      b26

      Description

      java.util.PrimitiveIterator::void forEachRemaining specification says:

       > Performs the given action for each remaining element, in the order elements occur when iterating, until all elements have been processed or the action throws an exception

      https://download.java.net/java/early_access/jdk17/docs/api/java.base/java/util/PrimitiveIterator.OfInt.html#forEachRemaining(java.util.function.IntConsumer)

      Code sample

      ===========================
      import java.util.PrimitiveIterator;
      import java.util.stream.IntStream;

      public class Test {
          public static void main(String... args) {
              PrimitiveIterator.OfInt iterator = IntStream.range(1, 5).iterator();
              try { iterator.forEachRemaining(
                      (int i) -> { if (i < 3) System.err.println(i); else throw new RuntimeException("Stop!"); });
              } catch (Exception e) {
                  System.err.println(e);
              }
              System.out.println("iterator.nextInt() = " + iterator.nextInt());
          }
      }
      ===========================

      Checking JDK17b23:

      > /17/23/jdk-17/bin/javac Test.java
      > /17/23/jdk-17/bin/java Test
      1
      2
      java.lang.RuntimeException: Stop!
      iterator.nextInt() = 4

      Checking JDK17b24:

      > /17/24/jdk-17/bin/javac Test.java
      > /17/24/jdk-17/bin/java Test

      1
      2
      java.lang.RuntimeException: Stop!
      Exception in thread "main" java.util.NoSuchElementException
      at java.base/java.util.Spliterators$2Adapter.nextInt(Spliterators.java:747)
      at Test.main(Test.java:12)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              psandoz Paul Sandoz
              Reporter:
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: