6356745: (coll) Add PriorityQueue(Collection, Comparator)

Co-authored-by: Valeh Hajiyev <valeh.hajiyev@gmail.com>
Reviewed-by: vklang, smarks, liach
This commit is contained in:
Liam Miller-Cushon 2026-06-25 08:30:07 +00:00
parent 3f03e104ed
commit 3b30a57e30
2 changed files with 123 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -209,6 +209,32 @@ public class PriorityQueue<E> extends AbstractQueue<E>
}
}
/**
* Creates a {@code PriorityQueue} containing the elements in the
* specified collection. The elements of the new {@code PriorityQueue}
* will be ordered according to the specified comparator.
*
* @param c the collection whose elements are to be placed
* into this priority queue
* @param comparator the comparator that will be used to order this
* priority queue. If {@code null}, the {@linkplain Comparable
* natural ordering} of the elements will be used.
* @throws NullPointerException if the specified collection or any
* of its elements are null
* @since 28
*/
public PriorityQueue(Collection<? extends E> c,
Comparator<? super E> comparator) {
this.comparator = comparator;
if (c instanceof SortedSet<? extends E> ss && comparator == ss.comparator()) {
initElementsFromCollection(ss);
} else if (c instanceof PriorityQueue<? extends E> pq && comparator == pq.comparator()) {
initFromPriorityQueue(pq);
} else {
initFromCollection(c);
}
}
/**
* Creates a {@code PriorityQueue} containing the elements in the
* specified priority queue. This priority queue will be

View File

@ -40,6 +40,7 @@ import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.TreeSet;
import junit.framework.Test;
@ -168,6 +169,101 @@ public class PriorityQueueTest extends JSR166TestCase {
mustEqual(items[i], q.poll());
}
/**
* Queue contains all elements of collection used to initialize and
* uses the custom comparator provided to order its elements
*/
public void testConstructor8() {
Item[] items = seqItems(SIZE);
MyReverseComparator cmp = new MyReverseComparator();
@SuppressWarnings("unchecked")
PriorityQueue<Item> q = new PriorityQueue<>(Arrays.asList(items), cmp);
assertEquals(cmp, q.comparator());
for (int i = SIZE - 1; i >= 0; --i)
mustEqual(items[i], q.poll());
}
/**
* Initializing from Collection with a comparator has the order
* of its elements the same as the queue initialized with a comparator
* and populated with Collection after initialization
*/
public void testConstructor9() {
Item[] items = seqItems(SIZE);
MyReverseComparator cmp = new MyReverseComparator();
@SuppressWarnings("unchecked")
PriorityQueue<Item> q1 = new PriorityQueue<>(Arrays.asList(items), cmp);
@SuppressWarnings("unchecked")
PriorityQueue<Item> q2 = new PriorityQueue<>(SIZE, cmp);
q2.addAll(Arrays.asList(items));
for (int i = 0; i < SIZE; ++i)
mustEqual(q1.poll(), q2.poll());
}
/**
* Initializing from null Collection throws NPE
*/
public void testConstructor10() {
try {
new PriorityQueue<Item>((Collection<Item>)null, new MyReverseComparator());
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Initializing from Collection of null elements throws NPE
*/
public void testConstructor11() {
try {
new PriorityQueue<Item>(Arrays.asList(new Item[SIZE]), new MyReverseComparator());
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Initializing from PriorityQueue and its comparator
*/
public void testConstructor12() {
Item[] items = seqItems(SIZE);
MyReverseComparator cmp = new MyReverseComparator();
@SuppressWarnings("unchecked")
PriorityQueue<Item> q1 = new PriorityQueue<>(cmp);
q1.addAll(Arrays.asList(items));
@SuppressWarnings("unchecked")
PriorityQueue<Item> q2 = new PriorityQueue<>(q1, q1.comparator());
for (int i = 0; i < SIZE; ++i)
mustEqual(q1.poll(), q2.poll());
}
/**
* Initializing from SortedSet and its comparator
*/
public void testConstructor13() {
Item[] items = seqItems(SIZE);
MyReverseComparator cmp = new MyReverseComparator();
@SuppressWarnings("unchecked")
TreeSet<Item> s = new TreeSet<>(cmp);
s.addAll(Arrays.asList(items));
@SuppressWarnings("unchecked")
PriorityQueue<Item> q = new PriorityQueue<>(s, s.comparator());
for (int i = 0; i < SIZE; ++i)
mustEqual(q.poll(), s.removeFirst());
}
/**
* Initializing with null comparator
*/
public void testConstructor14() {
Item[] items = seqItems(SIZE);
@SuppressWarnings("unchecked")
PriorityQueue<Item> q1 = new PriorityQueue<>((Comparator<Item>) null);
q1.addAll(Arrays.asList(items));
@SuppressWarnings("unchecked")
PriorityQueue<Item> q2 = new PriorityQueue<>(Arrays.asList(items), null);
for (int i = 0; i < SIZE; ++i)
mustEqual(q1.poll(), q2.poll());
}
/**
* isEmpty is true before add, false after
*/