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

  • Home
  • SEARCH
  • 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 8921299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:30:18+00:00 2026-06-15T06:30:18+00:00

PriorityQueue<Integer> queue = new PriorityQueue<Integer>(4); queue.add(8); queue.add(5); queue.add(23); queue.add(6); System.out.println(queue); Friends, The preceding code

  • 0
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(4);
queue.add(8);
queue.add(5);
queue.add(23);
queue.add(6);
System.out.println(queue);

Friends,
The preceding code on Ubuntu 12.10 and Oracle Java 1.6 and Java 1.7, it is printing output as

[5, 6, 23, 8]

I believe this is wrong. This should have instead printed as [5, 6, 8, 23]
Is this a defect? or my understanding of priority queue is wrong?

In addition to it, if I change the position of adding 23 to PriorityQueue before or after, this works as expected.

  • 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-15T06:30:19+00:00Added an answer on June 15, 2026 at 6:30 am

    The toString() method for the AbstractCollection class (which is what PriorityQueue uses) states that it:

    returns a string representation of this collection. The string representation consists of a list of the collection’s elements in the order they are returned by its iterator, enclosed in square brackets (“[]”).

    If you look at the docs for the PriorityQueue iterator, it states:

    Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

    The priority only comes in to play when you extract items from the queue, not when you get a string representation of them.

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

Sidebar

Related Questions

I have priority queue in Java of Integers: PriorityQueue<Integer> pq= new PriorityQueue<Integer>(); When I
java.util.PriorityQueue allows a Comparator to be passed at construction time. When inserting elements, they
fringe = new PriorityQueue<Node>(10,new Comparator<Node>(){ @Override public int compare(Node node1,Node node2) { if (f(node1)>f(node2))
In my other question You can see code of my arr structure and PriorityQueue
Take the PriorityQueue for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E) Can anyone give me an example of a
If I want to print out values in a PriorityQueue , how can I
I was given a program which uses integer input to calculate Priority Queue. How
I need to use a priority queue in my Python code, and: am looking
Is it possible in Java to create a PriorityQueue of objects where the key
I'd like to wrap java's PriorityQueue class in clojure for use in another part

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.