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

Code example provided by RandomGeneratorFactory does not compile

XMLWordPrintable

    • b20
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      JDK17 Windows 10 21H1 64bit

      A DESCRIPTION OF THE PROBLEM :
      In https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/RandomGeneratorFactory.html the code provided as:
      RandomGeneratorFactory<RandomGenerator> best = RandomGeneratorFactory.all()
               .sorted(Comparator.comparingInt(RandomGenerator::stateBits).reversed())
               .findFirst()
               .orElse(RandomGeneratorFactory.of("Random"));
      does not compile. The correct code appears to include:
          .sorted(Comparator.comparingInt(RandomGeneratorFactory<RandomGenerator>::stateBits).reversed())


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile the example code provided in the documentation using JDK17.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The example code should compile.
      ACTUAL -
      The example code does not compile.

      ---------- BEGIN SOURCE ----------
        public static void main(final String[] args)
        {
          RandomGeneratorFactory<RandomGenerator> best = RandomGeneratorFactory.all()
              .sorted(Comparator.comparingInt(RandomGenerator::stateBits).reversed())
              // .sorted(Comparator.comparingInt(RandomGeneratorFactory<RandomGenerator>::stateBits).reversed()) //<----- correct code
              .findFirst()
              .orElse(RandomGeneratorFactory.of("Random"));
          System.out.println(best.name() + " in " + best.group() + " was selected");

          RandomGenerator rng = best.create();
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Correct the example code.

      FREQUENCY : always


            jlaskey Jim Laskey
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: