Class PriorityQueue<E extends Comparable<E>>
java.lang.Object
gaiasky.event.PriorityQueue<E>
- Type Parameters:
E- Object type.
Simple implementation of a priority queue.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates aPriorityQueuewith the default initial capacity (11) that orders its elements according to their natural ordering.PriorityQueue(int initialCapacity) Creates aPriorityQueuewith the specified initial capacity that orders its elements according to their natural ordering. -
Method Summary
Modifier and TypeMethodDescriptionbooleanInserts the specified element into this priority queue.voidclear()Removes all of the elements from this priority queue.get(int index) Retrieves the element at the specified index.booleanReturns a value indicating whether only unique elements are allowed to be inserted.peek()Retrieves, but does not remove, the head of this queue.poll()Retrieves and removes the head of this queue, or returnsnullif this queue is empty.voidsetUniqueness(boolean uniqueness) Sets a flag indicating whether only unique elements are allowed to be inserted.intsize()Returns the number of elements in this queue.
-
Constructor Details
-
PriorityQueue
public PriorityQueue()Creates aPriorityQueuewith the default initial capacity (11) that orders its elements according to their natural ordering. -
PriorityQueue
public PriorityQueue(int initialCapacity) Creates aPriorityQueuewith the specified initial capacity that orders its elements according to their natural ordering.- Parameters:
initialCapacity- the initial capacity for this priority queue
-
-
Method Details
-
getUniqueness
public boolean getUniqueness()Returns a value indicating whether only unique elements are allowed to be inserted. -
setUniqueness
public void setUniqueness(boolean uniqueness) Sets a flag indicating whether only unique elements are allowed to be inserted. -
add
Inserts the specified element into this priority queue. Ifuniquenessis enabled and this priority queue already contains the element, the call leaves the queue unchanged and returns false.- Returns:
- true if the element was added to this queue, else false
- Throws:
ClassCastException- if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's orderingNullPointerException- if the specified element is null
-
peek
Retrieves, but does not remove, the head of this queue. If this queue is emptynullis returned.- Returns:
- the head of this queue
-
get
Retrieves the element at the specified index. If such an element doesn't existnullis returned.Iterating the queue by index is not guaranteed to traverse the elements in any particular order.
- Returns:
- the element at the specified index in this queue.
-
size
public int size()Returns the number of elements in this queue. -
clear
public void clear()Removes all of the elements from this priority queue. The queue will be empty after this call returns. -
poll
Retrieves and removes the head of this queue, or returnsnullif this queue is empty.- Returns:
- the head of this queue, or
nullif this queue is empty.
-