Skip to content

Commit

Permalink
BAEL-5921 | Guide to Java Priority Queue (eugenp#13087)
Browse files Browse the repository at this point in the history
* BAEL-5921 | Guide to Java Priority Queue

* BAEL-5921 | Adressed comments

* BAEL-5921 | Fixed test

* BAEL-5921 | Fixed test
  • Loading branch information
GaetanoPiazzolla authored Nov 27, 2022
1 parent 3a35086 commit 2881826
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 2881826

Please sign in to comment.