[Ask] PriorityQueue on Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[Ask] PriorityQueue on Java

Can you help me to explain about order sequence of PriorityQueue with simple words. cause so confuse when i try to add same elements with different input order the result is difference . please see two codes below.Exactly what is the meaning of "Priority" here? Thanks Before code 1: https://code.sololearn.com/cZYQLnwZKPtC/?ref=app code 2: https://code.sololearn.com/cLpY2kON3tuk/?ref=app

24th May 2017, 1:13 PM
DraST (dradroid.com)
DraST (dradroid.com) - avatar
2 Answers
+ 4
Hi, PriorityQueue uses natural ordering. You can ask then why the printed result is in different order. Because it is just a toString() method so it is a mistake you tried to understand this queue using it. You should poll items from the queue: String s=null; while((s=data.poll()) != null) { System.out.println(s); } Try it on your code.
24th May 2017, 5:14 PM
Tamás Barta
Tamás Barta - avatar
+ 1
Thanks a lot @Tamás Barta, Sorry for my mistakes to understand because i still beginer😄. but your code was solved my problem. Thanks again..👍
24th May 2017, 5:45 PM
DraST (dradroid.com)
DraST (dradroid.com) - avatar