Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:13:21+00:00 2026-06-08T19:13:21+00:00

I am trying to implement PriorityQueue. This PriorityQueue is going to hold instances of

  • 0

I am trying to implement PriorityQueue. This PriorityQueue is going to hold instances of class Task. These instances of Task should be arranged in such a way that the instances which have higher “priority” are at the head of the Queue. In short, instances should be in descending order of priority.

    private static Queue<Task> testQ = new PriorityQueue<Task>(11, new TaskQueueComparator());


    /*** Comparator ***/        
    public class TaskQueueComparator implements Comparator<Task> {

    public int compare(Task task1, Task task2) {
        return task2.priority - task1.priority;
        }        
    }

    /**** Task definition **/       
    public class Task {
        public int priority;
        }


    /**** Code in main() ****/

    Task pe11 = new Task();
    pe11.priority = 3;
    testQ.add(pe11);


    pe11 = new Task();
    pe11.priority = 1;
    testQ.add(pe11);


    pe11 = new Task();
    pe11.priority = 2;
    testQ.add(pe11);


    void displayQueue() {

    int size = testQ.size();

    for (int k = 0; k < size; k++)
    {
        Task p = testQ.poll();
        System.out.format("Task Priority %d \n", p.priority); // The result I am getting is  3 1 2.. I was expecting 3 2 1
    }

As shown in the comment, this outputs 3,1,2 instead of 3,2,1 like I was expecting. Can someone please let me know what mistake I am doing here ? Every time I remove or add a task from /to the queue, the queue should arrange the tasks in descending order of priority.

Let me know.

Thanks
Josh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:13:22+00:00Added an answer on June 8, 2026 at 7:13 pm

    FYI, PriorityQueue only returns the elements in priority order when you poll() the Queue. I found this one out the hard way when I tired iterating over it a long time ago. Additionally, it only performs the compare on insertion. So if your priority changes while in the queue you are going to get very odd behavior.

    By changing your code to the following:

    void displayQueue() {
        while (!testQ.isEmpty())
        {   
        Task p = testQ.poll(); // poll, you want to remove the head
        System.out.format("Task Priority %d \n", p.priority);
        }
    }
    

    I was able to get:

    Task Priority 3

    Task Priority 2

    Task Priority 1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
Trying to implement a search similar to here .This searches properties based on city,locality,property
Trying to implement a function that will return a list of ints the represent
I'm trying implement a way to recursively template using jsRender. The issue is, my
I have this weird kind of error. I am trying implement basic Euclidean algorithm
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
Trying to implement a shell, mainly piping. I've written this test case which I
Am trying to implement a generic way for reading sections from a config file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.