Skip to content

Commit c4b34c1

Browse files
authored
Bael 7853 (#16503)
* BAEL-7853: Removed unnecessary dependencies * BAEL-7853: Simple PriorityQueue tests * BAEL-7853: Additional example with Meetings
1 parent eca1d49 commit c4b34c1

4 files changed

Lines changed: 280 additions & 12 deletions

File tree

core-java-modules/core-java-collections-6/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,12 @@
1414
</parent>
1515

1616
<dependencies>
17-
<dependency>
18-
<groupId>org.junit.platform</groupId>
19-
<artifactId>junit-platform-runner</artifactId>
20-
<version>${junit-platform.version}</version>
21-
<scope>test</scope>
22-
</dependency>
2317
<dependency>
2418
<groupId>org.junit.jupiter</groupId>
2519
<artifactId>junit-jupiter</artifactId>
2620
<version>${junit.version}</version>
2721
<scope>test</scope>
2822
</dependency>
29-
<dependency>
30-
<groupId>org.junit.vintage</groupId>
31-
<artifactId>junit-vintage-engine</artifactId>
32-
<version>${junit.version}</version>
33-
<scope>test</scope>
34-
</dependency>
3523
<dependency>
3624
<groupId>org.roaringbitmap</groupId>
3725
<artifactId>RoaringBitmap</artifactId>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.priorityqueuewithpair;
2+
3+
public class Book {
4+
5+
private final String author;
6+
private final String title;
7+
private final int publicationYear;
8+
9+
public Book(String author, String title, int publicationYear) {
10+
this.author = author;
11+
this.title = title;
12+
this.publicationYear = publicationYear;
13+
}
14+
15+
public String getAuthor() {
16+
return author;
17+
}
18+
19+
public String getTitle() {
20+
return title;
21+
}
22+
23+
public int getPublicationYear() {
24+
return publicationYear;
25+
}
26+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.priorityqueuewithpair;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.Objects;
5+
6+
public class Meeting implements Comparable<Meeting> {
7+
8+
private final LocalDateTime startTime;
9+
private final LocalDateTime endTime;
10+
11+
private final String title;
12+
13+
public Meeting(LocalDateTime startTime, LocalDateTime endTime, String title) {
14+
this.startTime = startTime;
15+
this.endTime = endTime;
16+
this.title = title;
17+
}
18+
19+
public LocalDateTime getStartTime() {
20+
return startTime;
21+
}
22+
23+
public LocalDateTime getEndTime() {
24+
return endTime;
25+
}
26+
27+
public String getTitle() {
28+
return title;
29+
}
30+
31+
@Override
32+
public boolean equals(Object object) {
33+
if (this == object) {
34+
return true;
35+
}
36+
if (object == null || getClass() != object.getClass()) {
37+
return false;
38+
}
39+
40+
Meeting meeting = (Meeting) object;
41+
42+
if (!Objects.equals(startTime, meeting.startTime)) {
43+
return false;
44+
}
45+
if (!Objects.equals(endTime, meeting.endTime)) {
46+
return false;
47+
}
48+
return Objects.equals(title, meeting.title);
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
int result = startTime != null ? startTime.hashCode() : 0;
54+
result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
55+
result = 31 * result + (title != null ? title.hashCode() : 0);
56+
return result;
57+
}
58+
59+
@Override
60+
public int compareTo(Meeting meeting) {
61+
return this.startTime.compareTo(meeting.startTime);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.baeldung.priorityqueuewithpair;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.AbstractMap;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.PriorityQueue;
12+
import java.util.stream.Stream;
13+
import org.apache.commons.math3.util.Pair;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.params.ParameterizedTest;
16+
import org.junit.jupiter.params.provider.MethodSource;
17+
18+
class PriorityQueueWithComplexObjectUnitTest {
19+
20+
@Test
21+
void givenMeetings_whenUseWithPriorityQueue_thenSortByStartDateTime() {
22+
Meeting projectDiscussion = new Meeting(
23+
LocalDateTime.parse("2025-11-10T19:00:00"),
24+
LocalDateTime.parse("2025-11-10T20:00:00"),
25+
"Project Discussion"
26+
);
27+
Meeting businessMeeting = new Meeting(
28+
LocalDateTime.parse("2025-11-15T14:00:00"),
29+
LocalDateTime.parse("2025-11-15T16:00:00"),
30+
"Business Meeting"
31+
);
32+
PriorityQueue<Meeting> meetings = new PriorityQueue<>();
33+
meetings.add(projectDiscussion);
34+
meetings.add(businessMeeting);
35+
36+
assertThat(meetings.poll()).isEqualTo(projectDiscussion);
37+
assertThat(meetings.poll()).isEqualTo(businessMeeting);
38+
}
39+
40+
@ParameterizedTest
41+
@MethodSource("pairProvider")
42+
void givenPairs_whenUsePriorityQueue_thenSortThemBySecondElement(List<Pair<String, Integer>> pairs) {
43+
PriorityQueue<Pair<String, Integer>> queue = new PriorityQueue<>(Comparator.comparingInt(Pair::getSecond));
44+
45+
queue.addAll(pairs);
46+
Pair<String, Integer> previousEntry = queue.poll();
47+
while (!queue.isEmpty()) {
48+
Pair<String, Integer> currentEntry = queue.poll();
49+
assertThat(previousEntry.getSecond()).isLessThanOrEqualTo(currentEntry.getSecond());
50+
previousEntry = currentEntry;
51+
}
52+
}
53+
54+
@ParameterizedTest
55+
@MethodSource("mapEntryProvider")
56+
void givenMapEntries_whenUsePriorityQueue_thenSortThemBySecondElement(List<Map.Entry<String, Integer>> pairs) {
57+
PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue));
58+
59+
queue.addAll(pairs);
60+
Map.Entry<String, Integer> previousEntry = queue.poll();
61+
while (!queue.isEmpty()) {
62+
Map.Entry<String, Integer> currentEntry = queue.poll();
63+
assertThat(previousEntry.getValue()).isLessThanOrEqualTo(currentEntry.getValue());
64+
previousEntry = currentEntry;
65+
}
66+
}
67+
68+
@ParameterizedTest
69+
@MethodSource("bookProvider")
70+
void givenBooks_whenUsePriorityQueue_thenSortThemBySecondElement(List<Book> books) {
71+
PriorityQueue<Book> queue = new PriorityQueue<>(Comparator.comparingInt(Book::getPublicationYear));
72+
queue.addAll(books);
73+
Book previousBook = queue.poll();
74+
while (!queue.isEmpty()) {
75+
Book currentBook = queue.poll();
76+
assertThat(previousBook.getPublicationYear())
77+
.isLessThanOrEqualTo(currentBook.getPublicationYear());
78+
previousBook = currentBook;
79+
}
80+
}
81+
82+
@ParameterizedTest
83+
@MethodSource("bookProvider")
84+
void givenBooks_whenUsePriorityQueueWithoutComparator_thenThrowClassCastExcetption(List<Book> books) {
85+
PriorityQueue<Book> queue = new PriorityQueue<>();
86+
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> queue.addAll(books));
87+
}
88+
89+
@ParameterizedTest
90+
@MethodSource("bookProvider")
91+
void givenBooks_whenUsePriorityQueueWithoutComparatorWithoutAddingElements_thenNoExcetption(List<Book> books) {
92+
PriorityQueue<Book> queue = new PriorityQueue<>();
93+
assertThat(queue).isNotNull();
94+
}
95+
96+
static Stream<List> pairProvider() {
97+
return Stream.of(
98+
List.of(
99+
new Pair<>("Dune", 1965),
100+
new Pair<>("Foundation", 1951),
101+
new Pair<>("2001: A Space Odyssey", 1968),
102+
new Pair<>("Do Androids Dream of Electric Sheep?", 1968),
103+
new Pair<>("Ender's Game", 1985),
104+
new Pair<>("The Hitchhiker's Guide to the Galaxy", 1979),
105+
new Pair<>("1984", 1949),
106+
new Pair<>("Brave New World", 1932),
107+
new Pair<>("Fahrenheit 451", 1953),
108+
new Pair<>("Neuromancer", 1984),
109+
new Pair<>("Stranger in a Strange Land", 1961),
110+
new Pair<>("The Handmaid's Tale", 1985),
111+
new Pair<>("Ubik", 1969),
112+
new Pair<>("Solaris", 1961),
113+
new Pair<>("Slaughterhouse-Five", 1969),
114+
new Pair<>("The War of the Worlds", 1898),
115+
new Pair<>("Childhood's End", 1953),
116+
new Pair<>("Snow Crash", 1992),
117+
new Pair<>("The Moon is a Harsh Mistress", 1966),
118+
new Pair<>("Hyperion", 1989),
119+
new Pair<>("The Left Hand of Darkness", 1969),
120+
new Pair<>("The Gods Themselves", 1972),
121+
new Pair<>("The Stars My Destination", 1956),
122+
new Pair<>("Rendezvous with Rama", 1973),
123+
new Pair<>("The Forever War", 1974)
124+
)
125+
);
126+
}
127+
128+
static Stream<List> mapEntryProvider() {
129+
return Stream.of(
130+
List.of(
131+
new AbstractMap.SimpleEntry<>("Dune", 1965),
132+
new AbstractMap.SimpleEntry<>("Foundation", 1951),
133+
new AbstractMap.SimpleEntry<>("2001: A Space Odyssey", 1968),
134+
new AbstractMap.SimpleEntry<>("Do Androids Dream of Electric Sheep?", 1968),
135+
new AbstractMap.SimpleEntry<>("Ender's Game", 1985),
136+
new AbstractMap.SimpleEntry<>("The Hitchhiker's Guide to the Galaxy", 1979),
137+
new AbstractMap.SimpleEntry<>("1984", 1949),
138+
new AbstractMap.SimpleEntry<>("Brave New World", 1932),
139+
new AbstractMap.SimpleEntry<>("Fahrenheit 451", 1953),
140+
new AbstractMap.SimpleEntry<>("Neuromancer", 1984),
141+
new AbstractMap.SimpleEntry<>("Stranger in a Strange Land", 1961),
142+
new AbstractMap.SimpleEntry<>("The Handmaid's Tale", 1985),
143+
new AbstractMap.SimpleEntry<>("Ubik", 1969),
144+
new AbstractMap.SimpleEntry<>("Solaris", 1961),
145+
new AbstractMap.SimpleEntry<>("Slaughterhouse-Five", 1969),
146+
new AbstractMap.SimpleEntry<>("The War of the Worlds", 1898),
147+
new AbstractMap.SimpleEntry<>("Childhood's End", 1953),
148+
new AbstractMap.SimpleEntry<>("Snow Crash", 1992),
149+
new AbstractMap.SimpleEntry<>("The Moon is a Harsh Mistress", 1966),
150+
new AbstractMap.SimpleEntry<>("Hyperion", 1989),
151+
new AbstractMap.SimpleEntry<>("The Left Hand of Darkness", 1969),
152+
new AbstractMap.SimpleEntry<>("The Gods Themselves", 1972),
153+
new AbstractMap.SimpleEntry<>("The Stars My Destination", 1956),
154+
new AbstractMap.SimpleEntry<>("Rendezvous with Rama", 1973),
155+
new AbstractMap.SimpleEntry<>("The Forever War", 1974)
156+
)
157+
);
158+
}
159+
160+
static Stream<List<Book>> bookProvider() {
161+
return Stream.of(
162+
List.of(
163+
new Book("Frank Herbert", "Dune", 1965),
164+
new Book("Isaac Asimov", "Foundation", 1951),
165+
new Book("Arthur C. Clarke", "2001: A Space Odyssey", 1968),
166+
new Book("Philip K. Dick", "Do Androids Dream of Electric Sheep?", 1968),
167+
new Book("Orson Scott Card", "Ender's Game", 1985),
168+
new Book("Douglas Adams", "The Hitchhiker's Guide to the Galaxy", 1979),
169+
new Book("George Orwell", "1984", 1949),
170+
new Book("Aldous Huxley", "Brave New World", 1932),
171+
new Book("Ray Bradbury", "Fahrenheit 451", 1953),
172+
new Book("William Gibson", "Neuromancer", 1984),
173+
new Book("Robert A. Heinlein", "Stranger in a Strange Land", 1961),
174+
new Book("Margaret Atwood", "The Handmaid's Tale", 1985),
175+
new Book("Philip K. Dick", "Ubik", 1969),
176+
new Book("Stanislaw Lem", "Solaris", 1961),
177+
new Book("Kurt Vonnegut", "Slaughterhouse-Five", 1969),
178+
new Book("H.G. Wells", "The War of the Worlds", 1898),
179+
new Book("Arthur C. Clarke", "Childhood's End", 1953),
180+
new Book("Neal Stephenson", "Snow Crash", 1992),
181+
new Book("Robert A. Heinlein", "The Moon is a Harsh Mistress", 1966),
182+
new Book("Dan Simmons", "Hyperion", 1989),
183+
new Book("Ursula K. Le Guin", "The Left Hand of Darkness", 1969),
184+
new Book("Isaac Asimov", "The Gods Themselves", 1972),
185+
new Book("Alfred Bester", "The Stars My Destination", 1956),
186+
new Book("Arthur C. Clarke", "Rendezvous with Rama", 1973),
187+
new Book("Joe Haldeman", "The Forever War", 1974)
188+
)
189+
);
190+
}
191+
}

0 commit comments

Comments
 (0)