Top Description Fields Constructors Methods
org.apache.commons.collections4.bag

public Class TreeBag<E>

extends AbstractMapBag<E>
implements SortedBag<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.collections4.SortedBag, org.apache.commons.collections4.Bag, java.util.Collection, java.lang.Iterable
Type Parameters
<E>
the type of elements in this bag
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Collection, .Comparator, .SortedMap, .TreeMap, org.apache.commons.collections4.SortedBag

Implements SortedBag, using a TreeMap to provide the data storage. This is the standard implementation of a sorted bag.

Order will be maintained among the bag members and can be viewed through the iterator.

A Bag stores each object in the collection together with a count of occurrences. Extra methods on the interface allow multiple copies of an object to be added or removed at once. It is important to read the interface javadoc carefully as several methods violate the Collection interface specification.

Since
3.0 (previously in main package v2.0)

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Serial version lock

Constructor Summary

AccessConstructor and Description
public
TreeBag()

Constructs an empty TreeBag.

public
TreeBag(final Comparator<? super E>
the comparator to use
comparator
)

Constructs an empty bag that maintains order on its unique representative members according to the given Comparator.

public
TreeBag(final Collection<? extends E>
the collection to copy into the bag
coll
)

Constructs a TreeBag containing all the members of the specified collection.

Method Summary

Modifier and TypeMethod and Description
public boolean
add(final E
the object to add
object
)

Overrides org.apache.commons.collections4.bag.AbstractMapBag.add.

Implements org.apache.commons.collections4.Bag.add.

(Violation) Adds one copy of the specified object to the Bag.
public Comparator<? super E>
comparator()

Implements org.apache.commons.collections4.SortedBag.comparator.

Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.
public E
first()

Implements org.apache.commons.collections4.SortedBag.first.

Returns the first (lowest) member.
protected SortedMap<E, AbstractMapBag.MutableInteger>
getMap()

Overrides org.apache.commons.collections4.bag.AbstractMapBag.getMap.

Utility method for implementations to access the map that backs this bag.
public E
last()

Implements org.apache.commons.collections4.SortedBag.last.

Returns the last (highest) member.
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the bag in using a custom routine.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the bag out using a custom routine.

Inherited from org.apache.commons.collections4.bag.AbstractMapBag:
addaddAllclearcontainscontainsAllcontainsAlldoReadObjectdoWriteObjectequalsgetCounthashCodeisEmptyiteratorremoveremoveremoveAllretainAllretainAllsizetoArraytoArraytoStringuniqueSet

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Serial version lock

Constructor Detail

TreeBagback to summary
public TreeBag()

Constructs an empty TreeBag.

TreeBagback to summary
public TreeBag(final Comparator<? super E> comparator)

Constructs an empty bag that maintains order on its unique representative members according to the given Comparator.

Parameters
comparator:Comparator<? super E>

the comparator to use

TreeBagback to summary
public TreeBag(final Collection<? extends E> coll)

Constructs a TreeBag containing all the members of the specified collection.

Parameters
coll:Collection<? extends E>

the collection to copy into the bag

Method Detail

addback to summary
public boolean add(final E object)

Overrides org.apache.commons.collections4.bag.AbstractMapBag.add.

Implements org.apache.commons.collections4.Bag.add.

Doc from org.apache.commons.collections4.Bag.add.

(Violation) Adds one copy of the specified object to the Bag.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as 1.

Since this method always increases the size of the bag, according to the Collection#add(Object) contract, it should always return true. Since it sometimes returns false, this method violates the contract.

Parameters
object:E

the object to add

Returns:boolean

true if the object was not already in the uniqueSet

Annotations
@Override
Exceptions
IllegalArgumentException:
if the object to be added does not implement Comparable and the TreeBag is using natural ordering
NullPointerException:
if the specified key is null and this bag uses natural ordering, or its comparator does not permit null keys
comparatorback to summary
public Comparator<? super E> comparator()

Implements org.apache.commons.collections4.SortedBag.comparator.

Doc from org.apache.commons.collections4.SortedBag.comparator.

Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.

Returns:Comparator<? super E>

the comparator in use, or null if natural ordering

Annotations
@Override
firstback to summary
public E first()

Implements org.apache.commons.collections4.SortedBag.first.

Doc from org.apache.commons.collections4.SortedBag.first.

Returns the first (lowest) member.

Returns:E

the first element in the sorted bag

Annotations
@Override
getMapback to summary
protected SortedMap<E, AbstractMapBag.MutableInteger> getMap()

Overrides org.apache.commons.collections4.bag.AbstractMapBag.getMap.

Doc from org.apache.commons.collections4.bag.AbstractMapBag.getMap.

Utility method for implementations to access the map that backs this bag. Not intended for interactive use outside of subclasses.

Returns:SortedMap<E, AbstractMapBag.MutableInteger>

the map being used by the Bag

Annotations
@Override
lastback to summary
public E last()

Implements org.apache.commons.collections4.SortedBag.last.

Doc from org.apache.commons.collections4.SortedBag.last.

Returns the last (highest) member.

Returns:E

the last element in the sorted bag

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the bag in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the bag out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream