Results 1 to 7 of 7
  1. #1
    sachin is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Unhappy PriorityQueue add method, adds the latest element only

    I am trying to create a priority Queue and using a comparator, but when I add the object, the last one is added and the earlier ones do not appear in the queue. Below is the code:

    public class CustomerCompare implements Comparator<Customer>{
    @Override
    public int compare(Customer c1, Customer c2) {
    if(c1.priority>c2.priority)
    return 1;
    if(c1.priority<c2.priority)
    return -1;
    return 0;

    }
    }

    public class PQueue {


    public static void main(String[] args) {
    Comparator<Customer> comparator = new CustomerCompare();
    PriorityQueue<Customer> queue = new PriorityQueue<Customer>(5, comparator);
    queue.add(new Customer("c1", 1));
    System.out.println("Queue is now :" + queue);
    queue.add(new Customer("c2", 7));
    System.out.println("Queue is now :" + queue);
    queue.add(new Customer("c3", 3));
    System.out.println("Queue is now :" + queue);
    queue.add(new Customer("c4", 6));
    System.out.println("Queue is now :" + queue);
    queue.add(new Customer("c5", 5));


    System.out.println("Elements in queue");
    while (true) {
    Customer currentCust = queue.poll();
    if (currentCust == null) {
    break;
    }
    System.out.print(currentCust.getCustomerNum() + " <-- ");
    }
    }
    }

    The output I get is:
    Queue is now :[Customer{c1 1}]
    Queue is now :[Customer{c2 7}, Customer{c2 7}]
    Queue is now :[Customer{c3 3}, Customer{c3 3}, Customer{c3 3}]
    Queue is now :[Customer{c4 6}, Customer{c4 6}, Customer{c4 6}, Customer{c4 6}]
    Elements in queue c5 <-- c5 <-- c5 <-- c5 <-- c5

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: PriorityQueue add method, adds the latest element only

    First, please put your code between [code][/code] tags. And also include *all* code. Where is the Customer class?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: PriorityQueue add method, adds the latest element only

    Well, I guessed at the fields and created my own Customer class. Your code works for me! So I have to ask if your priority field is an int? Or perhaps you are not assigning priority in your constructor or it is getting changed somehow. I also duplicated your output by declaring the Customer name and priority as static fields. They should be instance variables.

    Jim
    Last edited by jim829; 03-01-2013 at 06:16 AM. Reason: another possibility
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    sachin is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: PriorityQueue add method, adds the latest element only

    Firstly, sorry for not providing the customer class earlier. I have the priority field as int and am assigning it in constructor itself. Below is the customer class, did you create something like this or am I doing something wrong. What output did you get?

    public class Customer{

    private static String customerNum;
    static int priority;

    public Customer(String custNum, int priority) {
    customerNum=custNum;
    priority = priority;

    }

    public static String getCustomerNum() {
    return customerNum;
    }

    public static int getPriority() {
    return priority;
    }

    @Override
    public String toString() {
    return "Customer{"+customerNum+" "+priority + '}';
    }
    }

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: PriorityQueue add method, adds the latest element only

    The problem is as I eventually suspected. You need to change your fields to non-static. Otherwise, they will get overwritten for each instance you create.

    e.g

    not this
    Java Code:
    private static String customerNum;
         static int priority;
    but this

    Java Code:
    private String customerNum;
    int priority;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    sachin is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: PriorityQueue add method, adds the latest element only

    Great. That worked! Thanks so much.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: PriorityQueue add method, adds the latest element only

    You're most welcome!

    Jim
    Last edited by jim829; 03-01-2013 at 06:39 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 3
    Last Post: 11-29-2012, 05:33 AM
  2. Correctly passing an element to a method
    By aianta in forum New To Java
    Replies: 5
    Last Post: 10-28-2011, 07:52 PM
  3. Replies: 17
    Last Post: 07-19-2011, 12:53 AM
  4. Replies: 1
    Last Post: 01-06-2011, 09:56 PM
  5. Replies: 5
    Last Post: 10-30-2010, 12:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •