File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PriorityQueue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PriorityQueue" Watch "PriorityQueue" New topic


sumi selva

Joined: Jul 10, 2007
Posts: 19
I executed the following code.
import java.util.*;

class DemoPQ {
public static void main(String[] args) {
PriorityQueue<String> pq = new PriorityQueue<String>();

for(String s q) {
System.out.print(pq.peek() + ", " + pq.poll());



Got the output as Amit followed by ConcurrentModificationException.
May I know why does this exception come?

Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
I think polling(removing element) causing the Iteration get effected. try to poll outside the loop.

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
christian combarel
Ranch Hand

Joined: Aug 04, 2007
Posts: 47
For each iteration the current thread (main) read an element from the Queue and then alter it with the poll method.
So at the second iteration, the content of collection (pq) has changed and it's the cause of the throw of ConcurrentModificationException. Read the Javadoc of it.

If you write something like :

it works fine.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: PriorityQueue
jQuery in Action, 3rd edition