Skip to main content

PriorityQueue

Basics

  • Implements Queue

Construction

  • By Comparator class:
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b){
return a-b;
}
});
  • By Lambda:
PriorityQueue<Integer> pq = new PriorityQueue<>((Integer a, Integer b) -> b-a);

Methods

MethodRuntimeDescriptionReturn Type
add(E e)O(log n)Appends the specified element to the end of this list.boolean
clear()O(n)removes all elementvoid
contains(Object o)O(n) worst timecontainsboolean
iterator()O(1)creates a iteratorIterator
offer(E e)O(log n)insert element (like add)boolean
peek()O(1)retreves the headE
poll()O(n)retrieves and remove the headE
remove(Object o)o(n)removes a single instance of the specified valueboolean
size()O(1)return the sizeint
toArray()/toArray(T[] a)O(n)return the array[]/ T[]