FileDocCategorySizeDatePackage
PriorityQueue.javaAPI DocAndroid 1.5 API13121Wed May 06 22:41:04 BST 2009java.util

PriorityQueue

public class PriorityQueue extends AbstractQueue implements Serializable
A PriorityQueue holds elements on a priority heap, which orders the elements according to their natural order or according to the comparator specified at construction time. If the queue uses natural ordering, only elements that are comparable are permitted to be inserted into the queue.

The least element of the specified ordering is stored at the head of the queue and the greatest element is stored at the tail of the queue.

A PriorityQueue is not synchronized. If multiple threads will have to access it concurrently, use the {@link java.util.concurrent.PriorityBlockingQueue}.

since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final int
DEFAULT_CAPACITY
private static final double
DEFAULT_INIT_CAPACITY_RATIO
private static final int
DEFAULT_CAPACITY_RATIO
private int
size
private Comparator
comparator
private transient E[]
elements
Constructors Summary
public PriorityQueue()
Constructs a priority queue with an initial capacity of 11 and natural ordering.

since
Android 1.0


                          
      
        this(DEFAULT_CAPACITY);
    
public PriorityQueue(int initialCapacity)
Constructs a priority queue with the specified capacity and natural ordering.

param
initialCapacity the specified capacity.
throws
IllegalArgumentException if the initialCapacity is less than 1.
since
Android 1.0

        this(initialCapacity, null);
    
public PriorityQueue(int initialCapacity, Comparator comparator)
Constructs a priority queue with the specified capacity and comparator.

param
initialCapacity the specified capacity.
param
comparator the specified comparator. If it is null, the natural ordering will be used.
throws
IllegalArgumentException if the initialCapacity is less than 1.
since
Android 1.0

        if (initialCapacity < 1) {
            throw new IllegalArgumentException();
        }
        elements = newElementArray(initialCapacity);
        this.comparator = comparator;
    
public PriorityQueue(Collection c)
Constructs a priority queue that contains the elements of a collection. The constructed priority queue has the initial capacity of 110% of the size of the collection. The queue uses natural ordering to order its elements.

param
c the collection whose elements will be added to the priority queue to be constructed.
throws
ClassCastException if any of the elements in the collection are not comparable.
throws
NullPointerException if any of the elements in the collection are null.
since
Android 1.0

        if (c instanceof PriorityQueue) {
            getFromPriorityQueue((PriorityQueue<? extends E>) c);
        } else if (c instanceof SortedSet) {
            getFromSortedSet((SortedSet<? extends E>) c);
        } else {
            initSize(c);
            addAll(c);
        }
    
public PriorityQueue(PriorityQueue c)
Constructs a priority queue that contains the elements of another priority queue. The constructed priority queue has the initial capacity of 110% of the specified one. Both priority queues have the same comparator.

param
c the priority queue whose elements will be added to the priority queue to be constructed.
since
Android 1.0

        getFromPriorityQueue(c);
    
public PriorityQueue(SortedSet c)
Constructs a priority queue that contains the elements of a sorted set. The constructed priority queue has the initial capacity of 110% of the size of the sorted set. The priority queue will have the same comparator as the sorted set.

param
c the sorted set whose elements will be added to the priority queue to be constructed.
since
Android 1.0

        getFromSortedSet(c);
    
Methods Summary
public booleanadd(E o)
Adds the specified object to the priority queue.

param
o the object to be added.
return
always true.
throws
ClassCastException if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
since
Android 1.0

        return offer(o);
    
public voidclear()
Removes all the elements of the priority queue.

since
Android 1.0

        Arrays.fill(elements, null);
        size = 0;
    
public java.util.Comparatorcomparator()
Gets the comparator of the priority queue.

return
the comparator of the priority queue or null if the natural ordering is used.
since
Android 1.0

        return comparator;
    
private intcompare(E o1, E o2)

        if (null != comparator) {
            return comparator.compare(o1, o2);
        }
        return ((Comparable<? super E>) o1).compareTo(o2);
    
private voidgetFromPriorityQueue(java.util.PriorityQueue c)

        initSize(c);
        comparator = (Comparator<? super E>) c.comparator();
        System.arraycopy(c.elements, 0, elements, 0, c.size());
        size = c.size();
    
private voidgetFromSortedSet(java.util.SortedSet c)

        initSize(c);
        comparator = (Comparator<? super E>) c.comparator();
        Iterator<? extends E> iter = c.iterator();
        while (iter.hasNext()) {
            elements[size++] = iter.next();
        }
    
private voidgrowToSize(int size)

        if (size > elements.length) {
            E[] newElements = newElementArray(size * DEFAULT_CAPACITY_RATIO);
            System.arraycopy(elements, 0, newElements, 0, elements.length);
            elements = newElements;
        }
    
private voidinitSize(java.util.Collection c)

        if (null == c) {
            throw new NullPointerException();
        }
        if (c.isEmpty()) {
            elements = newElementArray(1);
        } else {
            int capacity = (int) Math.ceil(c.size()
                    * DEFAULT_INIT_CAPACITY_RATIO);
            elements = newElementArray(capacity);
        }
    
public java.util.Iteratoriterator()
Gets the iterator of the priority queue, which will not return elements in any specified ordering.

return
the iterator of the priority queue.
since
Android 1.0

        return new PriorityIterator();
    
private E[]newElementArray(int capacity)

        return (E[]) new Object[capacity];
    
public booleanoffer(E o)
Inserts the element to the priority queue.

param
o the element to add to the priority queue.
return
always true
throws
ClassCastException if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
since
Android 1.0

        if (null == o) {
            throw new NullPointerException();
        }
        growToSize(size + 1);
        elements[size] = o;
        siftUp(size++);
        return true;
    
public Epeek()
Gets but does not remove the head of the queue.

return
the head of the queue or null if the queue is empty.
since
Android 1.0

        if (isEmpty()) {
            return null;
        }
        return elements[0];
    
public Epoll()
Gets and removes the head of the queue.

return
the head of the queue or null if the queue is empty.
since
Android 1.0

        if (isEmpty()) {
            return null;
        }
        E result = elements[0];
        removeAt(0);
        return result;
    
private voidreadObject(java.io.ObjectInputStream in)

        in.defaultReadObject();
        int capacity = in.readInt();
        elements = newElementArray(capacity);
        for (int i = 0; i < size; i++) {
            elements[i] = (E) in.readObject();
        }
    
public booleanremove(java.lang.Object o)
Removes the specified object from the priority queue.

param
o the object to be removed.
return
true if the object was in the priority queue, false if the object was not in the priority queue.
since
Android 1.0

        if (o == null) {
            return false;
        }
        int targetIndex;
        for (targetIndex = 0; targetIndex < size; targetIndex++) {
            if (0 == this.compare((E) o, elements[targetIndex])) {
                break;
            }
        }
        if (size == 0 || size == targetIndex) {
            return false;
        }
        removeAt(targetIndex);
        return true;
    
private voidremoveAt(int index)

        size--;
        elements[index] = elements[size];
        siftDown(index);
        elements[size] = null;
    
private voidsiftDown(int rootIndex)

        E target = elements[rootIndex];
        int childIndex;
        while ((childIndex = rootIndex * 2 + 1) < size) {
            if (childIndex + 1 < size
                    && compare(elements[childIndex + 1], elements[childIndex]) < 0) {
                childIndex++;
            }
            if (compare(target, elements[childIndex]) <= 0) {
                break;
            }
            elements[rootIndex] = elements[childIndex];
            rootIndex = childIndex;
        }
        elements[rootIndex] = target;
    
private voidsiftUp(int childIndex)

        E target = elements[childIndex];
        int parentIndex;
        while (childIndex > 0) {
            parentIndex = (childIndex - 1) / 2;
            E parent = elements[parentIndex];
            if (compare(parent, target) <= 0) {
                break;
            }
            elements[childIndex] = parent;
            childIndex = parentIndex;
        }
        elements[childIndex] = target;
    
public intsize()
Gets the size of the priority queue. If the size of the queue is greater than the Integer.MAX, then it returns Integer.MAX.

return
the size of the priority queue.
since
Android 1.0

        return size;
    
private voidwriteObject(java.io.ObjectOutputStream out)

        out.defaultWriteObject();
        out.writeInt(elements.length);
        for (int i = 0; i < size; i++) {
            out.writeObject(elements[i]);
        }