public class PriorityBuffer extends java.util.AbstractCollection implements Buffer, java.io.Serializable
Buffer
that provides for
removal based on Comparator
ordering.
The removal order of a binary heap is based on either the natural sort
order of its elements or a specified Comparator
. The
remove()
method always returns the first element as determined
by the sort order. (The ascendingOrder
flag in the constructors
can be used to reverse the sort order, in which case remove()
will always remove the last element.) The removal order is
not the same as the order of iteration; elements are
returned by the iterator in no particular order.
The add(Object)
and remove()
operations perform
in logarithmic time. The get()
operation performs in constant
time. All other operations perform in linear time or worse.
Note that this implementation is not synchronized. Use
BufferUtils.synchronizedBuffer(Buffer)
or
SynchronizedBuffer.decorate(Buffer)
to provide synchronized access to a PriorityBuffer
:
Buffer heap = SynchronizedBuffer.decorate(new PriorityBuffer());
This class is Serializable from Commons Collections 3.2.
Constructor and Description |
---|
PriorityBuffer()
Constructs a new empty buffer that sorts in ascending order by the
natural order of the objects added.
|
PriorityBuffer(boolean ascendingOrder)
Constructs a new empty buffer specifying the sort order and using the
natural order of the objects added.
|
PriorityBuffer(boolean ascendingOrder,
java.util.Comparator comparator)
Constructs a new empty buffer specifying the sort order and comparator.
|
PriorityBuffer(java.util.Comparator comparator)
Constructs a new empty buffer that sorts in ascending order using the
specified comparator.
|
PriorityBuffer(int capacity)
Constructs a new empty buffer that sorts in ascending order by the
natural order of the objects added, specifying an initial capacity.
|
PriorityBuffer(int capacity,
boolean ascendingOrder)
Constructs a new empty buffer that specifying initial capacity and
sort order, using the natural order of the objects added.
|
PriorityBuffer(int capacity,
boolean ascendingOrder,
java.util.Comparator comparator)
Constructs a new empty buffer that specifying initial capacity,
sort order and comparator.
|
PriorityBuffer(int capacity,
java.util.Comparator comparator)
Constructs a new empty buffer that sorts in ascending order using the
specified comparator and initial capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(java.lang.Object element)
Adds an element to the buffer.
|
void |
clear()
Clears all elements from the buffer.
|
java.util.Comparator |
comparator()
Gets the comparator being used for this buffer, null is natural order.
|
java.lang.Object |
get()
Gets the next element to be removed without actually removing it (peek).
|
boolean |
isAscendingOrder()
Checks whether the heap is ascending or descending order.
|
java.util.Iterator |
iterator()
Returns an iterator over this heap's elements.
|
java.lang.Object |
remove()
Gets and removes the next element (pop).
|
int |
size()
Returns the number of elements in this buffer.
|
java.lang.String |
toString()
Returns a string representation of this heap.
|
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
public PriorityBuffer()
public PriorityBuffer(java.util.Comparator comparator)
comparator
- the comparator used to order the elements,
null means use natural orderpublic PriorityBuffer(boolean ascendingOrder)
ascendingOrder
- if true
the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heappublic PriorityBuffer(boolean ascendingOrder, java.util.Comparator comparator)
ascendingOrder
- true to use the order imposed by the given
comparator; false to reverse that ordercomparator
- the comparator used to order the elements,
null means use natural orderpublic PriorityBuffer(int capacity)
capacity
- the initial capacity for the buffer, greater than zerojava.lang.IllegalArgumentException
- if capacity
is <= 0
public PriorityBuffer(int capacity, java.util.Comparator comparator)
capacity
- the initial capacity for the buffer, greater than zerocomparator
- the comparator used to order the elements,
null means use natural orderjava.lang.IllegalArgumentException
- if capacity
is <= 0
public PriorityBuffer(int capacity, boolean ascendingOrder)
capacity
- the initial capacity for the buffer, greater than zeroascendingOrder
- if true
the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heap.java.lang.IllegalArgumentException
- if capacity
is <= 0
public PriorityBuffer(int capacity, boolean ascendingOrder, java.util.Comparator comparator)
capacity
- the initial capacity for the buffer, greater than zeroascendingOrder
- true to use the order imposed by the given
comparator; false to reverse that ordercomparator
- the comparator used to order the elements,
null means use natural orderjava.lang.IllegalArgumentException
- if capacity
is <= 0
public boolean isAscendingOrder()
public java.util.Comparator comparator()
public int size()
size
in interface java.util.Collection
size
in class java.util.AbstractCollection
public void clear()
clear
in interface java.util.Collection
clear
in class java.util.AbstractCollection
public boolean add(java.lang.Object element)
The element added will be sorted according to the comparator in use.
add
in interface java.util.Collection
add
in class java.util.AbstractCollection
element
- the element to be addedpublic java.lang.Object get()
get
in interface Buffer
BufferUnderflowException
- if the buffer is emptypublic java.lang.Object remove()
remove
in interface Buffer
BufferUnderflowException
- if the buffer is emptypublic java.util.Iterator iterator()
iterator
in interface java.lang.Iterable
iterator
in interface java.util.Collection
iterator
in class java.util.AbstractCollection
public java.lang.String toString()
toString
in class java.util.AbstractCollection
Copyright © 2010 - 2020 Adobe. All Rights Reserved