-4

I have an class PersonQ

public class PersonQ {
    Queue <Person> queue;
    int Rows;

    public PersonQ(int r){
        queue = new PriorityQueue<Person>(); 
        Rows = r; 
    }

    //Also I have getters and setters and a method to fill randomly with Random
}

And now

public class Person implements Comparable <Person> {
    String name; 
    int id; 
    double salary; 
    int position;

    public Person(String pN, int pID, double pSal, int pPos) {
        this.name = pN; 
        this.id = pID; 
        this.salary= pSal; 
        position=pPos;
    }

//Also I have getters and setters 

    @Override
    public int compareTo(Person other) {
        /*There I don't know how to make my statements because at the moment when I remove an element 
        it sorts wrong; Look, when I add 5 elements randomly it gets its proper position: 1, 2, 3, 4 
        and the last 5 when I use the method poll() it removes the element with position 1 but the 
        next step is that it sorts in a weird way, for example sometimes it starts with the element 
        with position 5, 4, 2 or 3 it occurs randomly and I want to see it in their proper order by its position */
    }
}

I want to show my Queue in this order after remove an element, If I remove an element with position 1, then the rest of them must appear like this: 2,3,4,5 and if I remove another it has to appear: 3,4,5 and so on. I tried with "Integer.compare(this.position, other.position);" And is the same

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Are you trying to compare by id or something else? – luckydog32 Nov 07 '17 at 05:06
  • How are you verifying that your order is wrong? – Robby Cornelissen Nov 07 '17 at 05:07
  • 3
    Please read [mcve] and enhance your question accordingly. – GhostCat Nov 07 '17 at 05:09
  • And also imp the formatting of your question. Indentation matters, and that lengthy comment inside your code is close to unreadable on mobile devices. – GhostCat Nov 07 '17 at 05:10
  • Question is unclear because you've not said what you are supposed to be comparing. Position is not a meaningful property of any element - Does a Person need to know its own `position`? – OneCricketeer Nov 07 '17 at 05:11
  • Please loop over the queue in that way: `while(!queue.isEmpty()){ Person p = queue.poll(); }` – UninformedUser Nov 07 '17 at 05:21
  • guys I made a Overview of everything, and I'm sure it shows wrong because I saw it in the debug – Hector Miguel Soto Nov 07 '17 at 13:09
  • In debug my queue it's fine when I add elements, that's why I add an additional value called position to prove it, so when I use a method like remove() or poll() my queue itself in debug mode change everything. For example an queue made by 5 elements each element has it's position: 1, 2, 3, 4, 5, so when I delete one element obviously it'll delete the head from the queue and then my new queue must has this order: 2, 3, 4, 5 (Start with 2 instead of another position selected randomly). This is my whole question how to keep the order by my var position. – Hector Miguel Soto Nov 07 '17 at 13:18
  • I use Iterator only to show info but correct me if I'm wrong is there any method or something to order my queue, because I don't think the fact that after remove an element with a method the new queue will have random positions – Hector Miguel Soto Nov 07 '17 at 13:28

2 Answers2

0

Not enough information to know for sure, but I'm guessing that you're using the Iterator returned by the PriorityQueue.iterator() method to verify the ordering of your queue.

Now, the JavaDoc of that method clearly states (emphasis mine):

Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Yes, I use all that stuff but in debug it shows the wrong position – Hector Miguel Soto Nov 07 '17 at 13:07
  • In debug my queue it's fine when I add elements, that's why I add an additional value called position to prove it, so when I use a method like remove() or poll() my queue itself in debug mode change everything. For example an queue made by 5 elements each element has it's position: 1, 2, 3, 4, 5, so when I delete one element obviously it'll delete the head from the queue and then my new queue must has this order: 2, 3, 4, 5 (Start with 2 instead of another position selected randomly). This is my whole question how to keep the order by var position. – Hector Miguel Soto Nov 07 '17 at 13:14
0

To use java.util.PriorityQueue, your class need to implement Comparable or specify Comparator when create an instance of PriorityQueue. The elements in queue will be sorted from low value to high value, the element will be evaluated by Comparator.compare or Comparable.compareTo method.

In your case, your class Person implements Comparable<Person>, you have to determine the order of Person. This implementation will make the elements order by position:

@Override
public int compareTo(Person other) {
    return position - other.position;
}

When you use PriorityQueue.poll(), it will give you the least value element.

Note: java.util.PriorityQueue and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()) (From Java doc)

Nam Tran
  • 643
  • 4
  • 14
  • Nice, You were close, but with this: return position - other.position; It does well with the first and last one but in the middle it still doing wrong, but don't worry I did it: return other.position -position; this did it backwards – Hector Miguel Soto Nov 07 '17 at 13:46