aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PriorityQueue FIFOEntry and Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PriorityQueue FIFOEntry and Generics" Watch "PriorityQueue FIFOEntry and Generics" New topic

PriorityQueue FIFOEntry and Generics

manco capac

Joined: Dec 11, 2004
Posts: 7

I am studying Java 5 Collections and Generics and came to PriorityQueue. I wrote a simple program
to see how it worked and found it does not work quite the way I had expected. Specifically,
I had assumed that things of equal priority would be in FIFO order on the queue, but my output
showed otherwise. I went back to the API and found:
... If multiple elements are tied for least value, the
head is one of those elements -- ties are broken arbitrarily. ..
I then came across PriorityBlockingQueue,
which has an example of how to solve this problem: FIFOEntry. Unfortunately, I didn't really know
Generics. I then finished reading CH7 section on Generics and thought I understood it.

I was able to make FIFOEntry work with:
PriorityQueue<FIFOEntry> mePq3 = new PriorityQueue<FIFOEntry>();
but when I tried to use FIFOEntry for :
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
I could not get it to compile. See line 129 in MyEvent Class below

I am getting the following error messages:

Description Resource Path Location Type
The constructor PriorityQueue<FIFOEntry>(int, InvPriorityCmp) is undefined PrioityQueueTest/src/com/timr/event line 129 Java Problem

Multiple markers at this line
- FIFOEntry is a raw type. References to generic type FIFOEntry<E> should be parameterized
- The constructor PriorityQueue<FIFOEntry>(int, InvPriorityCmp) is undefined
- FIFOEntry is a raw type. References to generic type FIFOEntry<E> should be parameterized

I tried several things to get it to compile but I realized I was just hacking and dont really understand
how to use generics in a slightly tricky example like this one.

Can anyone tell me what I am doing wrong?


The following is the output listing from my previous step when it did compile:

Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
I have never seen a Queue question that involved much more than
the three methods offer(), poll(), and peek(), and that the default
sorting is natural order, and that PriorityQueue is an implementation
class, and maybe that LinkedList also implements Queue.

I have never seen any question that attempted to actually define a
prioritizing scheme and implement it. That's cool that you got so
into it, but you seem to have bitten off more than you could chew,
and more than you needed to chew for the purposes of this exam.

SCJP 88% | SCWCD 84%
manco capac

Joined: Dec 11, 2004
Posts: 7

thanks for your reply. You are probably right this sort of thing won't be on the test.
But, I think my question has as much to do with Generics as it does with the PriorityQueue.
And it tells me I really don't know or understand Generics.

I am hoping somebody can set me straight on this. When I think about actually using a
PriorityQueue, I don't see where it would do me much good without the FIFOEntry solution.

Perhaps the question should be moved to a different forum...

again thanks for the quick reply,
manco capac

Joined: Dec 11, 2004
Posts: 7

I figured out the problem, my generics were wrong for the comparator.
It took me way too long to figure this out, generics are very tricky when
you get past the simple stuff.

You are correct this is beyond SCJP, maybe this should be moved to another

I found this site that may be a good source for others dealing with <Generics> and
having problems:
Generics FAQ

Thanks again, hope this helps somebody out,

changes to

It is sorta covered in the JavaRanch Style Guide.
subject: PriorityQueue FIFOEntry and Generics
Similar Threads
Priority queue
Priority queue
Priority Queue Ordering Question
Priority Queue and iterator