Guest User

Untitled

a guest
Feb 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.PriorityQueue;
  3. import java.util.Queue;
  4.  
  5. /**
  6. * Example class to illustrate the use of PriorityQueue
  7. * @author Solange U. Gasengayire
  8. */
  9. public class Example {
  10.  
  11. /**
  12. * Application entry point
  13. * @param args command-line arguments
  14. */
  15. public static void main(String[] args) {
  16.  
  17. Person mark = new Person("Mark", "Knopfler");
  18. Person debbie = new Person("Debbie", "Harry");
  19. Person chrissie = new Person("Chrissie", "Hynde");
  20. Person george = new Person("George", "Harrison");
  21.  
  22. PriorityQueue<Person> artists = new PriorityQueue<>(4);
  23. artists.add(debbie);
  24. artists.add(george);
  25. artists.add(mark);
  26. artists.add(chrissie);
  27.  
  28. System.out.println("Before any changes");
  29. System.out.println("******************");
  30. printQueue(artists);
  31.  
  32. debbie.firstname = "Ann";
  33. System.out.println("\nAfter change to element of the queue");
  34. System.out.println("************************************");
  35. printQueue(artists);
  36.  
  37. artists.add(debbie);
  38. System.out.println("\nAfter adding an already present element to the queue");
  39. System.out.println("****************************************************");
  40. printQueue(artists);
  41.  
  42. debbie.firstname = "Ann-Debbie";
  43. System.out.println("\nAfter 2nd change to element of the queue");
  44. System.out.println("****************************************");
  45. printQueue(artists);
  46.  
  47. }
  48.  
  49. /**
  50. * Print content of a queue
  51. * @param queue
  52. */
  53. private static void printQueue(Queue<Person> queue) {
  54. Iterator<Person> it = queue.iterator();
  55. while (it.hasNext()) {
  56. System.out.println("element of queue is: " + it.next());
  57. }
  58. }
  59. }
  60.  
  61. /**
  62. * Inner class to represent a Person
  63. * @author Solange U. Gasengayire
  64. */
  65. class Person implements Comparable<Person> {
  66. String firstname;
  67. String surname;
  68.  
  69. Person(String first, String name) {
  70. firstname = first;
  71. surname = name;
  72. }
  73.  
  74. @Override
  75. public int compareTo(Person o) {
  76. return firstname.compareTo(o.firstname);
  77. }
  78.  
  79. @Override
  80. public int hashCode() {
  81. final int prime = 31;
  82. int result = 1;
  83. result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
  84. result = prime * result + ((surname == null) ? 0 : surname.hashCode());
  85. return result;
  86. }
  87.  
  88. @Override
  89. public boolean equals(Object obj) {
  90. if (! (obj instanceof Person))
  91. return false;
  92.  
  93. Person n = (Person) obj;
  94. return n.firstname.equals(firstname)
  95. && n.surname.equals(surname);
  96. }
  97.  
  98. @Override
  99. public String toString() {
  100. return firstname + " " + surname;
  101. }
  102.  
  103. }
Add Comment
Please, Sign In to add comment