java.lang.Object | ||
↳ | java.util.AbstractCollection<E> | |
↳ | org.apache.commons.collections.buffer.PriorityBuffer |
Binary heap implementation of 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
synchronizedBuffer(Buffer)
or
decorate(Buffer)
to provide synchronized access to a PriorityBuffer
:
Buffer heap = SynchronizedBuffer.decorate(new PriorityBuffer());
This class is Serializable from Commons Collections 3.2.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ascendingOrder | If true, the first element as determined by the sort order will be returned. | ||||||||||
comparator | The comparator used to order the elements | ||||||||||
elements | The elements in this buffer. | ||||||||||
size | The number of elements currently in this buffer. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new empty buffer that sorts in ascending order by the
natural order of the objects added.
| |||||||||||
Constructs a new empty buffer that sorts in ascending order using the
specified comparator.
| |||||||||||
Constructs a new empty buffer specifying the sort order and using the
natural order of the objects added.
| |||||||||||
Constructs a new empty buffer specifying the sort order and comparator.
| |||||||||||
Constructs a new empty buffer that sorts in ascending order by the
natural order of the objects added, specifying an initial capacity.
| |||||||||||
Constructs a new empty buffer that sorts in ascending order using the
specified comparator and initial capacity.
| |||||||||||
Constructs a new empty buffer that specifying initial capacity and
sort order, using the natural order of the objects added.
| |||||||||||
Constructs a new empty buffer that specifying initial capacity,
sort order and comparator.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds an element to the buffer.
| |||||||||||
Clears all elements from the buffer.
| |||||||||||
Gets the comparator being used for this buffer, null is natural order.
| |||||||||||
Gets the next element to be removed without actually removing it (peek).
| |||||||||||
Checks whether the heap is ascending or descending order.
| |||||||||||
Returns an iterator over this heap's elements.
| |||||||||||
Gets and removes the next element (pop).
| |||||||||||
Returns the number of elements in this buffer.
| |||||||||||
Returns a string representation of this heap.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Compares two objects using the comparator if specified, or the
natural order otherwise.
| |||||||||||
Increases the size of the heap to support additional elements
| |||||||||||
Tests if the buffer is at capacity.
| |||||||||||
Percolates element down heap from the position given by the index.
| |||||||||||
Percolates element down heap from the position given by the index.
| |||||||||||
Percolates a new element up heap from the bottom.
| |||||||||||
Percolates element up heap from from the position given by the index.
| |||||||||||
Percolates a new element up heap from the bottom.
| |||||||||||
Percolates element up heap from the position given by the index.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() |
If true, the first element as determined by the sort order will be returned. If false, the last element as determined by the sort order will be returned.
The number of elements currently in this buffer.
Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added.
Constructs a new empty buffer that sorts in ascending order using the specified comparator.
comparator | the comparator used to order the elements, null means use natural order |
---|
Constructs a new empty buffer specifying the sort order and using the natural order of the objects added.
ascendingOrder | if true the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heap
|
---|
Constructs a new empty buffer specifying the sort order and comparator.
ascendingOrder | true to use the order imposed by the given comparator; false to reverse that order |
---|---|
comparator | the comparator used to order the elements, null means use natural order |
Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added, specifying an initial capacity.
capacity | the initial capacity for the buffer, greater than zero |
---|
IllegalArgumentException | if capacity is <= 0
|
---|
Constructs a new empty buffer that sorts in ascending order using the specified comparator and initial capacity.
capacity | the initial capacity for the buffer, greater than zero |
---|---|
comparator | the comparator used to order the elements, null means use natural order |
IllegalArgumentException | if capacity is <= 0
|
---|
Constructs a new empty buffer that specifying initial capacity and sort order, using the natural order of the objects added.
capacity | the initial capacity for the buffer, greater than zero |
---|---|
ascendingOrder | if true the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heap. |
IllegalArgumentException | if capacity is <= 0
|
---|
Constructs a new empty buffer that specifying initial capacity, sort order and comparator.
capacity | the initial capacity for the buffer, greater than zero |
---|---|
ascendingOrder | true to use the order imposed by the given comparator; false to reverse that order |
comparator | the comparator used to order the elements, null means use natural order |
IllegalArgumentException | if capacity is <= 0
|
---|
Adds an element to the buffer.
The element added will be sorted according to the comparator in use.
element | the element to be added |
---|
Clears all elements from the buffer.
Gets the comparator being used for this buffer, null is natural order.
Gets the next element to be removed without actually removing it (peek).
BufferUnderflowException | if the buffer is empty |
---|
Checks whether the heap is ascending or descending order.
Returns an iterator over this heap's elements.
Gets and removes the next element (pop).
BufferUnderflowException | if the buffer is empty |
---|
Returns the number of elements in this buffer.
Returns a string representation of this heap. The returned string is similar to those produced by standard JDK collections.
Compares two objects using the comparator if specified, or the natural order otherwise.
a | the first object |
---|---|
b | the second object |
Increases the size of the heap to support additional elements
Tests if the buffer is at capacity.
true
if buffer is full; false
otherwise.
Percolates element down heap from the position given by the index.
Assumes it is a maximum heap.
index | the index of the element |
---|
Percolates element down heap from the position given by the index.
Assumes it is a minimum heap.
index | the index for the element |
---|
Percolates a new element up heap from the bottom.
Assume it is a maximum heap.
element | the element |
---|
Percolates element up heap from from the position given by the index.
Assume it is a maximum heap.
index | the index of the element to be percolated up |
---|
Percolates a new element up heap from the bottom.
Assumes it is a minimum heap.
element | the element |
---|
Percolates element up heap from the position given by the index.
Assumes it is a minimum heap.
index | the index of the element to be percolated up |
---|