Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Why PriorityQueue gives such a strange behaviour? (https://www.askmehelpdesk.com/showthread.php?t=772742)

  • Oct 25, 2013, 09:59 PM
    pravingahukar
    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.

  • All times are GMT -7. The time now is 10:49 PM.