java.util

Class PriorityQueue<E>

Implemented Interfaces:
Collection<E>, Iterable<E>, Queue<E>, Serializable

public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable

Since:
1.5
See Also:
Serialized Form

Constructor Summary

PriorityQueue()
Constructor for use by subclasses.
PriorityQueue(E> c)
PriorityQueue(E> c)
PriorityQueue(E> c)
PriorityQueue(int cap)
PriorityQueue(int cap, E> comp)

Method Summary

boolean
addAll(E> c)
Adds all of the elements in the specified collection to this queue.
void
clear()
Removes all of the elements from this queue.
Iterator
iterator()
Returns an iterator for the collection.
boolean
offer(E o)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
E
peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E
poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
boolean
remove(Object o)
int
size()
Comparator
super E> comparator()

Methods inherited from class java.util.AbstractQueue<E>

add, addAll, clear, element, remove

Methods inherited from class java.util.AbstractCollection<E>

T[] toArray, add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toString

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

PriorityQueue

public PriorityQueue()
Constructor for use by subclasses.

PriorityQueue

public PriorityQueue(E> c)

PriorityQueue

public PriorityQueue(E> c)

PriorityQueue

public PriorityQueue(E> c)

PriorityQueue

public PriorityQueue(int cap)

PriorityQueue

public PriorityQueue(int cap,
                     E> comp)

Method Details

addAll

public boolean addAll(E> c)
Adds all of the elements in the specified collection to this queue. Attempts to addAll of a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

This implementation iterates over the specified collection, and adds each element returned by the iterator to this queue, in turn. A runtime exception encountered while trying to add an element (including, in particular, a null element) may result in only some of the elements having been successfully added when the associated exception is thrown.

Specified by:
addAll in interface Collection<E>
Overrides:
addAll in interface AbstractQueue<E>
Parameters:
c - collection containing elements to be added to this queue
Returns:
true if this queue changed as a result of the call
Throws:
ClassCastException - if the class of an element of the specified collection prevents it from being added to this queue
NullPointerException - if the specified collection contains a null element and this queue does not permit null elements, or if the specified collection is null
IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this queue, or if the specified collection is this queue
IllegalStateException - if not all the elements can be added at this time due to insertion restrictions
See Also:
add(Object)

clear

public void clear()
Removes all of the elements from this queue. The queue will be empty after this call returns.

This implementation repeatedly invokes poll until it returns null.

Specified by:
clear in interface Collection<E>
Overrides:
clear in interface AbstractQueue<E>

iterator

public Iterator iterator()
Returns an iterator for the collection.
Specified by:
iterator in interface Collection<E>
iterator in interface Iterable<E>
Overrides:
iterator in interface AbstractCollection<E>
Returns:
an iterator.

offer

public boolean offer(E o)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to Queue.add(E), which can fail to insert an element only by throwing an exception.
Specified by:
offer in interface Queue<E>
Parameters:
Returns:
true if the element was added to this queue, else false
Throws:
ClassCastException - if the class of the specified element prevents it from being added to this queue
NullPointerException - if the specified element is null and this queue does not permit null elements
IllegalArgumentException - if some property of this element prevents it from being added to this queue

peek

public E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Specified by:
peek in interface Queue<E>
Returns:
the head of this queue, or null if this queue is empty

poll

public E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
Specified by:
poll in interface Queue<E>
Returns:
the head of this queue, or null if this queue is empty

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<E>
Overrides:
remove in interface AbstractCollection<E>

size

public int size()
Specified by:
size in interface Collection<E>
Overrides:
size in interface AbstractCollection<E>

super E> comparator

public Comparatorsuper E> comparator()

PriorityQueue.java -- Unbounded priority queue Copyright (C) 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.