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

PriorityQueue remove() method returns incorrect value with lambda expression

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8, 11, 17
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      ADDITIONAL SYSTEM INFORMATION :
      macOS Catalina
      java version "1.8.0_212"
      Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
      Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      PriorityQueue remove() method returns incorrect value when using lambda expression to specify the comparator. However, when using Comparator.comparingInt it returns correct value.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      //RUN THE CODE BELOW. THE SMALLER VALUE -2147483648 SHOULD BE RETURNED IN BOTH CASES, //HOWEVER, WHEN USING A LAMBDA EXPRESSION THE LARGER OF THE TWO VALUES IS RETURNED


      package com.company.practice;

      import java.util.Comparator;
      import java.util.PriorityQueue;

      public class PriQueue {

          public static void main(String[] args) {
              PriorityQueue heap1 = new PriorityQueue<Integer>((a1, a2) -> (a1-a2));
              heap1.add(2147483647);
              heap1.add(-2147483648);
              System.out.println("Result from heap1: " + heap1.remove());

              PriorityQueue heap2 = new PriorityQueue<Integer>(Comparator.comparingInt(a -> a));
              heap2.add(2147483647);
              heap2.add(-2147483648);
              System.out.println("Result from heap2: " + heap2.remove());

          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Result from heap1: -2147483648
      Result from heap2: -2147483648
      ACTUAL -
      Result from heap1: 2147483647
      Result from heap2: -2147483648

      ---------- BEGIN SOURCE ----------
      package com.company.practice;

      import java.util.Comparator;
      import java.util.PriorityQueue;

      public class PriQueue {

          public static void main(String[] args) {
              PriorityQueue heap1 = new PriorityQueue<Integer>((a1, a2) -> (a1-a2));
              heap1.add(2147483647);
              heap1.add(-2147483648);
              System.out.println("Result from heap1: " + heap1.remove());

              PriorityQueue heap2 = new PriorityQueue<Integer>(Comparator.comparingInt(a -> a));
              heap2.add(2147483647);
              heap2.add(-2147483648);
              System.out.println("Result from heap2: " + heap2.remove());

          }
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            martin Martin Buchholz
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: