Java Reference
In-Depth Information
is an
int
value. Similarly, any floating-point literal that has the
suffix
F
is a
float
value.
A floating-point literal without a suffix is a
double
value. You can learn more about
numeric literals in the
Java Language Specification
at
http://docs.oracle.com/javase/
An infinite loop (lines 32-37) calls
Stack
method
pop
to remove the
top
element of
the stack. The method returns a
Number
reference to the removed element. If there are no
elements in the
Stack
, method
pop
throws an
EmptyStackException
, which terminates
the loop. Class
Stack
also declares
method
peek
. This method returns the
top
element of
the stack
without
popping the element off the stack.
Method
printStack
(lines 46-52) displays the stack's contents. The current
top
of
the stack (the last value pushed onto the stack) is the
first
value printed. Line 48 calls
Stack
method
isEmpty
(inherited by
Stack
from class
Vector
) to determine whether the stack
is empty. If it's empty, the method returns
true
; otherwise,
false
.
Recall that a queue is a collection that represents a waiting line—typically,
insertions
are
made at the back of a queue and
deletions
are made from the front. In Section 21.6, we'll
discuss and implement a queue data structure. In this section, we investigate Java's
Queue
interface and
PriorityQueue
class from package
java.util
. Interface
Queue
extends in-
terface
Collection
and provides additional operations for
inserting
,
removing
and
inspect-
ing
elements in a queue.
PriorityQueue
, which implements the
Queue
interface, orders
elements by their natural ordering as specified by
Comparable
elements'
compareTo
meth-
od or by a
Comparator
object that's supplied to the constructor.
Class
PriorityQueue
provides functionality that enables
insertions in sorted order
into
the underlying data structure and
deletions
from the
front
of the underlying data structure.
When adding elements to a
PriorityQueue
, the elements are inserted in priority order
such that the
highest-priority element
(i.e., the largest value) will be the first element
removed from the
PriorityQueue
.
The common
PriorityQueue
operations are
offer
to
insert
an element at the appro-
priate location based on priority order,
poll
to
remove
the highest-priority element of the
priority queue (i.e., the head of the queue),
peek
to get a reference to the highest-priority
element of the priority queue (without removing that element),
clear
to
remove all ele-
ments
in the priority queue and
size
to get the number of elements in the priority queue.
Figure 16.15 demonstrates the
PriorityQueue
class. Line 10 creates a
PriorityQueue
that stores
Double
s with an
initial capacity
of
11
elements and orders the elements according
to the object's natural ordering (the defaults for a
PriorityQueue
).
PriorityQueue
is a
generic class. Line 10 instantiates a
PriorityQueue
with a type argument
Double
. Class
PriorityQueue
provides five additional constructors. One of these takes an
int
and a
Com-
parator
object to create a
PriorityQueue
with the
initial capacity
specified by the
int
and
the
ordering
by the
Comparator
. Lines 13-15 use method
offer
to add elements to the pri-
ority queue. Method
offer
throws a
NullPointerException
if the program attempts to
add a
null
object to the queue. The loop in lines 20-24 uses method
size
to determine
whether the priority queue is
empty
(line 20). While there are more elements, line 22 uses
PriorityQueue
method
peek
to retrieve the
highest-priority element
in the queue for output
(
without
actually removing it from the queue). Line 23 removes the highest-priority ele-
ment in the queue with method
poll
, which returns the removed element.