PriorityQueue containing "Pairs" in reverse numerical order Java -


I am trying to create a preference that holds its elements behind the natural order (integer pairs) But we have found serious tips, but in every case it has given the same wrong order.

  priority question & lt; Pair & gt; Pq = New Priority Question (4, New Comparative & Lt; Pair & gt; () {Comparison of Public Entry (pair A1, pair A2) {return a2.value.compareTo (a1.value);}}); Pq.add (new pair (1,15)); Pq.add (new pair (2,58)); Pq.add (new pair (3,55)); Pq.add (new pair (723)); Iterator & LT; Pair & gt; This = pq.iterator (); While (this hasNext ()) {System.out.println (it.next (). Value); }   

There is a pair class

  applies public class jury Comparable {public Integer Name; Public integer value; Public pair (int name, int value) {this.name = name; This.value = value; } Public int getname () {return name; } Public receipt value () {return value; } Comparison of public int (pair o1, pair o2) {pair 1 = (pair) o 1; Pair A2 = (pair) O2; If (a1.value & gt; a2.value) {return1; } And if (a1.value & lt; a2.value) {return -1; } Return 0; } @ Override Public Ent Hashod () {Inti hash = 3; Return hash; } @ Override Public Boolean Parabar (object o) {pair A2 = (pair) o; This .name == a2.name & amp; Amp; This.value == a2.value; } To compute public int (object o) {pair a2 = (pair) o; If (this.value & gt; a2.value) {return1; } And if (this.value & lt; a2.value) {return -1; } Return 0; }   

}

If I use the "new priority question ()" constructor, then it gives proper natural order. Thank you for your time, mark

from the PriorityQueue.iterator ()

Returns an Iterator above the elements in this line The itater does not return the elements in a particular sequence.

If you want to prioritize them, then poll () code> empty :

  Couple pair; While ((pair = pq.poll ()) = tap) {System.out.println (pair.value); }   

She prints 58, 55, 23, 15, just as you were looking for.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -