BAEL-5921 | Guide to Java Priority Queue (#13087)

* BAEL-5921 | Guide to Java Priority Queue

* BAEL-5921 | Adressed comments

* BAEL-5921 | Fixed test

* BAEL-5921 | Fixed test
This commit is contained in:
Gaetano Piazzolla 2022-11-27 20:00:09 +01:00 committed by GitHub
parent d54d4768c0
commit 53dbea8d56
3 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.collections.priorityqueue;
public final class ColoredNumber {
private int value;
private String color;
public ColoredNumber(int value, String color) {
this.value = value;
this.color = color;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.collections.priorityqueue;
public final class ColoredNumberComparable implements Comparable<ColoredNumberComparable> {
private int value;
private String color;
public ColoredNumberComparable(int value, String color) {
this.value = value;
this.color = color;
}
@Override
public int compareTo(ColoredNumberComparable o) {
// (both numbers are red) or (both numbers are not red)
if ((this.color.equals("red") && o.color.equals("red")) ||
(!this.color.equals("red") && !o.color.equals("red"))) {
return Integer.compare(this.value, o.value);
}
// only the first number is red
else if (this.color.equals("red")) {
return -1;
}
// only the second number is red
else {
return 1;
}
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}

View File

@ -0,0 +1,74 @@
package com.baeldung.collections.priorityqueue;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.PriorityQueue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class PriorityQueueComparatorUnitTest {
@Test
void givenIntegerQueue_defaultComparator_followsNaturalOrdering() {
PriorityQueue<Integer> integerQueue = new PriorityQueue<>();
PriorityQueue<Integer> integerQueueWithComparator = new PriorityQueue<>((Integer c1, Integer c2) -> Integer.compare(c1, c2));
integerQueueWithComparator.add(3);
integerQueue.add(3);
integerQueueWithComparator.add(2);
integerQueue.add(2);
integerQueueWithComparator.add(1);
integerQueue.add(1);
assertThat(integerQueue.poll()).isEqualTo(1).isEqualTo(integerQueueWithComparator.poll());
assertThat(integerQueue.poll()).isEqualTo(2).isEqualTo(integerQueueWithComparator.poll());
assertThat(integerQueue.poll()).isEqualTo(3).isEqualTo(integerQueueWithComparator.poll());
}
@Test
void givenIntegerQueue_reverseOrderComparator_followsInverseNaturalOrdering() {
PriorityQueue<Integer> reversedQueue = new PriorityQueue<>(Collections.reverseOrder());
reversedQueue.add(1);
reversedQueue.add(2);
reversedQueue.add(3);
assertThat(reversedQueue.poll()).isEqualTo(3);
assertThat(reversedQueue.poll()).isEqualTo(2);
assertThat(reversedQueue.poll()).isEqualTo(1);
}
@Test
void givenNotComparableQueue_classCastException() {
assertThatThrownBy(() -> {
PriorityQueue<ColoredNumber> queue = new PriorityQueue<>();
queue.add(new ColoredNumber(3, "red"));
queue.add(new ColoredNumber(2, "blue"));
}).isInstanceOf(ClassCastException.class);
}
@Test
void givenCustomOrderingQueue_orderIsCorrect() {
PriorityQueue<ColoredNumberComparable> queue = new PriorityQueue<>();
queue.add(new ColoredNumberComparable(10, "red"));
queue.add(new ColoredNumberComparable(20, "red"));
queue.add(new ColoredNumberComparable(1, "blue"));
queue.add(new ColoredNumberComparable(2, "blue"));
ColoredNumberComparable first = queue.poll();
assertThat(first.getColor()).isEqualTo("red");
assertThat(first.getValue()).isEqualTo(10);
queue.poll();
ColoredNumberComparable third = queue.poll();
assertThat(third.getColor()).isEqualTo("blue");
assertThat(third.getValue()).isEqualTo(1);
}
}