I have a Collection (a Set to be specific) and want to create a PriorityQueue from it. I also want my custom comparator, but there’s no such constructor.
PriorityQueue(Collection c) PriorityQueue(int initialCapacity, Comparator comparator)
http://developer.android.com/reference/java/util/PriorityQueue.html
Is there any way to first create the PriorityQueue from the Collection and then add the Comparator later? If no, what’s the most efficient way to add it “manually”?
Basically you have two options:
Use the
PriorityQueue(Collection c)constructor and let the elements in the collection implement theComparableinterface. (And let the priority queue fall back on the natural ordering of the elements).Use the
PriorityQueue(int initialCapacity, Comparator comparator)constructor, and then add all the elements of the collection, preferrably using thePriorityQueue.addAll(Collection c)method.