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

(coll) Add PriorityQueue(Collection, Comparator)

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      PriorityQueue does not have a constructor that accept a Collection and a Comparator.

      JUSTIFICATION :
      Most users will users will the next lines
      Queue q = new PriorityQueue(comparate);
      q.addAll(collection);

      The running time for these lines is O(n log n), which defies a motivation to use such a priority queue.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      add the follwing method to the API.
      public PriorityQueue( Collection<? extends E> c, Comparator<? super E> comparator);
      to achieve an optimal O(n) running time
      ACTUAL -
      Suboptimal running time

      ---------- BEGIN SOURCE ----------
      Queue q = new PriorityQueue(comparate);
      q.addAll(collection);

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
        PriorityQueue is not final, but it is not designed (nor documented) for inheritance.
      Therefore the only bulletproof work around I found is to create an almost identical class with the same code and an additional constructor.
      public PriorityQueueOptimized(Collection<? extends E> collection, Comparator<? super E> comparator) {
              this.comparator = comparator;
              initializeArray(collection);
              fillFromUnsorted(collection);
          }

            liach Chen Liang
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: