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

inconsistent syntax error with Comparator.comparingInt.thenComparingInt

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      System.getProperty("java.version") = 23.0.1
      Eclipse Adoptium jdk-23.0.1.11-hotspot

      A DESCRIPTION OF THE PROBLEM :
      Getting syntax error for a perfectly valid declaration of Comparator. The syntax error is obtained while entering code in Eclipse 2024-09 or in VSCode jupyter notebook with Java kernel. The declaration involves Comparator.comparingInt(n -> f(n)).thenComparingInt(n -> h(n))

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      class Node {
          public int depth;
          public Node(int depth) {
              this.depth = depth;
          }
      }

      class Trial {
          public static int f(Node n) {
              return n.depth;
          }
          
          public static int h(Node n) {
              return 2 * n.depth;
          }

          private static PriorityQueue<Node> frontier1 = new PriorityQueue<>(Comparator.comparingInt(n -> f(n))); // no error
          private static PriorityQueue<Node> frontier2 = new PriorityQueue<>(Comparator.comparingInt(Trial::f)); // no error
          private static PriorityQueue<Node> frontier3 = new PriorityQueue<>(Comparator.comparingInt(n -> f(n)).thenComparingInt(n -> h(n))); // error
          private static PriorityQueue<Node> frontier4 = new PriorityQueue<>(Comparator.comparingInt(Trial::f).thenComparingInt(n -> h(n))); // no error
          private static PriorityQueue<Node> frontier5 = new PriorityQueue<>(Comparator.comparingInt(Trial::f).thenComparingInt(Trial::h)); // no error
              
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected no syntax error for frontier3 declaration
      ACTUAL -
      Error messages:
      '- The method f(Node) in the type Trial is not applicable for the arguments (Object)'
      'incompatible types: java.lang.Object cannot be converted to Node'

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: