Why PriorityQueue gives such a strange behaviour?



Share on Google+Share on Google+

Pravin Gahukar
Why PriorityQueue gives such a strange behaviour?
0 Answer(s)      3 years and 11 months ago
Posted in : Java Beginners

I am using priority Queue

Queue<Request> q = new PriorityQueue<Request>(5, new SortRequest());

My Request class contains two fields int id and int count along with setters and getters.

SortRequest class is something like this:

class SortRequest implements Comparator<Request>, Serializable{
   public int compare(Request r1, Request r2) {
                return new Integer(r1.getCount()).compareTo(r2.getCount());
                return new Integer(r1.getId()).compareTo(r2.getId());
           return 0; 

And also I am storing this queue in file after every add/poll operation using Object input/output stream. While adding element I am first reading stored entries and then adding new entry and again storing it back.

Constraint is that id should be unique, count can be same/different for two or more ids. So I'm performing sorting on count parameter, but in case if count is same then I should get the sort order such that lower id should precede higher one.

I tried initially this program on my linux system, where it was working properly and correctly giving sort order as per my expectation. But then somehow I deleted all class files and also created a new file for storing, and now same code(after recompiling) giving me completely strange results. Its not even sorting based on count parameter. Why this is so?

I then tried the same code on my windows 7 system, its again started producing correct results. Why such a strange behavior ? Please help. Thanks.

View Answers


Advertisement null