Ask Experts Questions for FREE Help !
Ask
    pravingahukar's Avatar
    pravingahukar Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 25, 2013, 09:59 PM
    Why PriorityQueue gives such a strange behaviour?
    I am using priority Queue

    Code:
    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:

    Code:
    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.

Check out some similar questions!

Husband's Strange behaviour [ 22 Answers ]

I am 33 years old with a 9 years old boy from my previous marriage and my husband 55 years old with a boy of 14 and a daughter of 10.My husband was married to an other one long time ago with a daughter and son of almost my age about which I didn't know before marriage.its an arranged marriage.I was...

Strange behaviour [ 6 Answers ]

My uncle is about 80 years old he has some strange behaviour and nobody can figure out what is wrong. He repeats sentences over and over, he will go into detail how he went to the washroom, or did other daily things. He keeps saying stuff like " do you want me to tell you how i went to the...

Cat strange behaviour [ 4 Answers ]

I moved house about five weeks ago and my cat settled in fine. Since the weekend just gone she has been acting very strangely - running away from me (which she never does, she is very affectionate), running around on her belly, scratching and wriggling when you pick her up and yowling (not...

Strange behaviour [ 4 Answers ]

Hey guys! I've been gone awhile but I'm back and armed with questions hehe. Okay so my Pomeranian puppy is now about 7 months old with a beautiful thick black coat. Being that we live in Arizona a week ago I shaved him as several Pom owners here do because he was so miserably hot. I especially...

My cats strange behaviour [ 2 Answers ]

My 4yr old cat sleeps on my bed at night. Recently she has started kneeding and licking me. We recently got a 12wk old puppy is this the reason for my cats strange behaviour.


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.