Class PriorityBuffer
- All Implemented Interfaces:
Serializable,Iterable,Collection,Buffer
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.
- Since:
- Commons Collections 3.0 (previously BinaryHeap v1.0)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added.PriorityBuffer(boolean ascendingOrder) Deprecated.Constructs a new empty buffer specifying the sort order and using the natural order of the objects added.PriorityBuffer(boolean ascendingOrder, Comparator comparator) Deprecated.Constructs a new empty buffer specifying the sort order and comparator.PriorityBuffer(int capacity) Deprecated.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) Deprecated.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, Comparator comparator) Deprecated.Constructs a new empty buffer that specifying initial capacity, sort order and comparator.PriorityBuffer(int capacity, Comparator comparator) Deprecated.Constructs a new empty buffer that sorts in ascending order using the specified comparator and initial capacity.PriorityBuffer(Comparator comparator) Deprecated.Constructs a new empty buffer that sorts in ascending order using the specified comparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanDeprecated.Adds an element to the buffer.voidclear()Deprecated.Clears all elements from the buffer.Deprecated.Gets the comparator being used for this buffer, null is natural order.get()Deprecated.Gets the next element to be removed without actually removing it (peek).booleanDeprecated.Checks whether the heap is ascending or descending order.iterator()Deprecated.Returns an iterator over this heap's elements.remove()Deprecated.Gets and removes the next element (pop).intsize()Deprecated.Returns the number of elements in this buffer.toString()Deprecated.Returns a string representation of this heap.Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Constructor Details
-
PriorityBuffer
public PriorityBuffer()Deprecated.Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added. -
PriorityBuffer
Deprecated.Constructs a new empty buffer that sorts in ascending order using the specified comparator.- Parameters:
comparator- the comparator used to order the elements, null means use natural order
-
PriorityBuffer
public PriorityBuffer(boolean ascendingOrder) Deprecated.Constructs a new empty buffer specifying the sort order and using the natural order of the objects added.- Parameters:
ascendingOrder- iftruethe heap is created as a minimum heap; otherwise, the heap is created as a maximum heap
-
PriorityBuffer
Deprecated.Constructs a new empty buffer specifying the sort order and comparator.- Parameters:
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 order
-
PriorityBuffer
public PriorityBuffer(int capacity) Deprecated.Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added, specifying an initial capacity.- Parameters:
capacity- the initial capacity for the buffer, greater than zero- Throws:
IllegalArgumentException- ifcapacityis <=0
-
PriorityBuffer
Deprecated.Constructs a new empty buffer that sorts in ascending order using the specified comparator and initial capacity.- Parameters:
capacity- the initial capacity for the buffer, greater than zerocomparator- the comparator used to order the elements, null means use natural order- Throws:
IllegalArgumentException- ifcapacityis <=0
-
PriorityBuffer
public PriorityBuffer(int capacity, boolean ascendingOrder) Deprecated.Constructs a new empty buffer that specifying initial capacity and sort order, using the natural order of the objects added.- Parameters:
capacity- the initial capacity for the buffer, greater than zeroascendingOrder- iftruethe heap is created as a minimum heap; otherwise, the heap is created as a maximum heap.- Throws:
IllegalArgumentException- ifcapacityis<= 0
-
PriorityBuffer
Deprecated.Constructs a new empty buffer that specifying initial capacity, sort order and comparator.- Parameters:
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 order- Throws:
IllegalArgumentException- ifcapacityis<= 0
-
-
Method Details
-
isAscendingOrder
public boolean isAscendingOrder()Deprecated.Checks whether the heap is ascending or descending order.- Returns:
- true if ascending order (a min heap)
-
comparator
Deprecated.Gets the comparator being used for this buffer, null is natural order.- Returns:
- the comparator in use, null is natural order
-
size
public int size()Deprecated.Returns the number of elements in this buffer.- Specified by:
sizein interfaceCollection- Specified by:
sizein classAbstractCollection- Returns:
- the number of elements in this buffer
-
clear
public void clear()Deprecated.Clears all elements from the buffer.- Specified by:
clearin interfaceCollection- Overrides:
clearin classAbstractCollection
-
add
Deprecated.Adds an element to the buffer.The element added will be sorted according to the comparator in use.
- Specified by:
addin interfaceCollection- Overrides:
addin classAbstractCollection- Parameters:
element- the element to be added- Returns:
- true always
-
get
Deprecated.Gets the next element to be removed without actually removing it (peek).- Specified by:
getin interfaceBuffer- Returns:
- the next element
- Throws:
BufferUnderflowException- if the buffer is empty
-
remove
Deprecated.Gets and removes the next element (pop).- Specified by:
removein interfaceBuffer- Returns:
- the next element
- Throws:
BufferUnderflowException- if the buffer is empty
-
iterator
Deprecated.Returns an iterator over this heap's elements.- Specified by:
iteratorin interfaceCollection- Specified by:
iteratorin interfaceIterable- Specified by:
iteratorin classAbstractCollection- Returns:
- an iterator over this heap's elements
-
toString
Deprecated.Returns a string representation of this heap. The returned string is similar to those produced by standard JDK collections.- Overrides:
toStringin classAbstractCollection- Returns:
- a string representation of this heap
-