Why PriorityQueue gives such a strange behaviour?

Why PriorityQueue gives such a strange behaviour?

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) {
           if(r1.getCount()!=r2.getCount())
                return new Integer(r1.getCount()).compareTo(r2.getCount());
           if(r1.getId()!=r2.getId())
                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









Related Tutorials/Questions & Answers:
Why PriorityQueue gives such a strange behaviour?
Why do the slashes change when the console gives me the error?
Advertisements
ModuleNotFoundError: No module named 'strange'
Version of com.mytechia>robobo-behaviours dependency
Strange JVM behaviour
Maven Dependency robobo-behaviours >> 0.1
Maven Dependency robobo-behaviours >> 0.1.1
Maven Dependency robobo-behaviours >> 0.3.0
Help please, some strange errors
Maven Repository/Dependency: com.mytechia | robobo-behaviours
com.mytechia - robobo-behaviours version 1.0.2 Maven dependency. How to use robobo-behaviours version 1.0.2 in pom.xml?
com.mytechia - robobo-behaviours version 1.0.4 Maven dependency. How to use robobo-behaviours version 1.0.4 in pom.xml?
Why request.getParameter(
Server_Gives_Null_Response
Why spring?
Maven dependency for com.mytechia - robobo-behaviours version 1.0.1 is released. Learn to use robobo-behaviours version 1.0.1 in Maven based Java projects
Maven dependency for com.mytechia - robobo-behaviours version 0.3.1-SAMSUNG is released. Learn to use robobo-behaviours version 0.3.1-SAMSUNG in Maven based Java projects
Why NSMutableDictionary
why this can't
Why this is not working...?
why jre in jdk
Why hibernate is used?
Why it called Struts?
why the php is open source?
why using static keyword
Why is super.super.method(); not allowed in Java?
why not float main?
why not float main?
Which gives better performace Stack vs Arraylist
Why Ajax for web?
Why "<![CDATA[ ]]>" CDATA tag.
Java is an opensource program or not? why???
ModuleNotFoundError: No module named 'why'
why get method is used?
why php and mysql - PHP
why crieterias are importent in hibernate?
Why Abstract Class?
Why Abstract Class?
why java is securable ?
Why Generics was introduced in Java?
Why Python is useful for AI?
Why is data science in demand?
Why is Big Data bad?
Why is Python so hard?
Why is Python popular?
Why is everyone Machine Learning?
Why data science?
Why NSString Objective C
why servlet as controller - Struts
Why is this code working

Ads