forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5921 | Guide to Java Priority Queue (eugenp#13087)
* BAEL-5921 | Guide to Java Priority Queue * BAEL-5921 | Adressed comments * BAEL-5921 | Fixed test * BAEL-5921 | Fixed test
- Loading branch information
1 parent
3a35086
commit 2881826
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ava-collections-4/src/main/java/com/baeldung/collections/priorityqueue/ColoredNumber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...tions-4/src/main/java/com/baeldung/collections/priorityqueue/ColoredNumberComparable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...src/test/java/com/baeldung/collections/priorityqueue/PriorityQueueComparatorUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|