'Java PriorityQueue no way to initialize with a Collection and custom comparator?
I read through the implementation of Java PriorityQueue, the constructors it has are:
PriorityQueue()
PriorityQueue(Collection<? extends E> c)
PriorityQueue(int initialCapacity)
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
PriorityQueue(PriorityQueue<? extends E> c)
PriorityQueue(SortedSet<? extends E> c)
You can see there is no way to initialize a heap with a collection and custom comparator.
Definitely I can create a heap with custom comparator and call 'offer' iteratively to add elements but the time complexity will be O(n*logn).
On the other hand, if the heap can be initialized with a collection(with custom comparator) and call 'siftDown', the time complexity can be lowered to O(logn). How to achieve that using Java PriorityQueue?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|