diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/priorityqueueiterator/Person.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/priorityqueueiterator/Person.java new file mode 100644 index 0000000000..887923e29d --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/priorityqueueiterator/Person.java @@ -0,0 +1,36 @@ +package com.baeldung.priorityqueueiterator; + +import java.util.Comparator; + +public class Person { + String name; + int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} + +class PersonAgeComparator implements Comparator { + @Override + public int compare(Person p1, Person p2) { + return p1.age - p2.age; // Ascending order + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/priorityqueueiterator/PriorityQueueMain.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/priorityqueueiterator/PriorityQueueMain.java new file mode 100644 index 0000000000..e366b63fb3 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/priorityqueueiterator/PriorityQueueMain.java @@ -0,0 +1,97 @@ +package com.baeldung.priorityqueueiterator; + +import java.util.Iterator; +import java.util.PriorityQueue; + +public class PriorityQueueMain { + + public static void main(String[] args) { + showPriorityQueueWithIterator(); + showCustomOrderingWithIterator(); + showCustomOrderingWithPoll(); + useIteratorToCapitalizeNames(); + showFailFastIteratorBehavior(); + } + + public static void showPriorityQueueWithIterator() { + PriorityQueue queue = new PriorityQueue<>(); + queue.add(3); + queue.add(1); + queue.add(2); + + Iterator iterator = queue.iterator(); + + while (iterator.hasNext()) { + Integer element = iterator.next(); + System.out.println(element); + } + } + + public static void showFailFastIteratorBehavior() { + PriorityQueue numbers = new PriorityQueue<>(); + numbers.add(3); + numbers.add(1); + numbers.add(2); + + Iterator iterator = numbers.iterator(); + numbers.add(4); + + try { + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } catch (java.util.ConcurrentModificationException e) { + System.out.println("ConcurrentModificationException occurred during iteration."); + } + } + + public static void showCustomOrderingWithIterator() { + // Creating a PriorityQueue with a custom comparator + PriorityQueue priorityQueue = new PriorityQueue<>(new PersonAgeComparator()); + + // Adding persons to the PriorityQueue + priorityQueue.add(new Person("Alice", 25)); + priorityQueue.add(new Person("Bob", 30)); + priorityQueue.add(new Person("Charlie", 22)); + + Iterator iterator = priorityQueue.iterator(); + + while (iterator.hasNext()) { + Person person = iterator.next(); + System.out.println("Name: " + person.name + ", Age: " + person.age); + } + } + + public static void showCustomOrderingWithPoll() { + PriorityQueue priorityQueue = new PriorityQueue<>(new PersonAgeComparator()); + + priorityQueue.add(new Person("Alice", 25)); + priorityQueue.add(new Person("Bob", 30)); + priorityQueue.add(new Person("Charlie", 22)); + + Iterator iterator = priorityQueue.iterator(); + + while (!priorityQueue.isEmpty()) { + Person person = priorityQueue.poll(); + System.out.println("Name: " + person.name + ", Age: " + person.age); + } + } + + public static void useIteratorToCapitalizeNames() { + PriorityQueue priorityQueue = new PriorityQueue<>(new PersonAgeComparator()); + + priorityQueue.add(new Person("Alice", 25)); + priorityQueue.add(new Person("Bob", 30)); + priorityQueue.add(new Person("Charlie", 22)); + + Iterator iterator = priorityQueue.iterator(); + + while (iterator.hasNext()) { + Person person = iterator.next(); + person.setName(person.getName() + .toUpperCase()); + System.out.println("Name: " + person.name + ", Age: " + person.age); + } + } + +}