fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static class Pair{
  11. int key;
  12. int value;
  13.  
  14. Pair(int key,int value){
  15. this.key=key;
  16. this.value=value;
  17. }
  18. }
  19. public static void main (String[] args) throws java.lang.Exception
  20. {
  21. // your code goes here
  22. PriorityQueue<Pair> pq = new PriorityQueue<>((a,b)-> a.value - b.value);
  23. pq.add(new Pair(1,20));
  24. pq.add(new Pair(2,90));
  25. pq.add(new Pair(3,40));
  26. pq.add(new Pair(4,50));
  27. pq.add(new Pair(5,80));
  28. pq.add(new Pair(6,70));
  29. pq.add(new Pair(7,60));
  30. pq.add(new Pair(8,30));
  31. pq.add(new Pair(9,10));
  32. while(!pq.isEmpty()){
  33. Pair p = pq.poll();
  34. System.out.println(p.key+"->"+p.value);
  35. }
  36. }
  37. }
Success #stdin #stdout 0.15s 57468KB
stdin
Standard input is empty
stdout
9->10
1->20
8->30
3->40
4->50
7->60
6->70
5->80
2->90