Top Description Methods
org.apache.commons.collections4

public Interface Bag<E>

extends Collection<E>
Known Direct Subinterfaces
org.apache.commons.collections4.SortedBag
Known Direct Implementers
org.apache.commons.collections4.bag.AbstractBagDecorator, org.apache.commons.collections4.bag.AbstractMapBag, org.apache.commons.collections4.bag.PredicatedBag, org.apache.commons.collections4.bag.SynchronizedBag, org.apache.commons.collections4.bag.TransformedBag
Type Parameters
<E>
the type of elements in this bag
Imports
java.util.Collection, .Iterator, .Set

Defines a collection that counts the number of times an object appears in the collection.

Suppose you have a Bag that contains {a, a, b, c}. Calling getCount(Object) on a would return 2, while calling uniqueSet() would return {a, b, c}.

NOTE: This interface violates the Collection contract. The behavior specified in many of these methods is not the same as the behavior specified by Collection. The noncompliant methods are clearly marked with "(Violation)". Exercise caution when using a bag as a Collection.

This violation resulted from the original specification of this interface. In an ideal world, the interface would be changed to fix the problems, however it has been decided to maintain backwards compatibility instead.

Since
2.0

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the object was not already in the uniqueSet
add
(E
the object to add
object
)

Redeclares java.util.Collection.add.

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

Returns:

true if the object was not already in the uniqueSet
add
(E
the object to add
object
,
int
the number of copies to add
nCopies
)

Adds nCopies copies of the specified object to the Bag.

public boolean

Returns:

true if the Bag contains all the collection
containsAll
(Collection<?>
the collection to check against
coll
)

Redeclares java.util.Collection.containsAll.

(Violation) Returns true if the bag contains all elements in the given collection, respecting cardinality.
public int

Returns:

the number of occurrences of the object, zero if not found
getCount
(Object
the object to search for
object
)

Returns the number of occurrences (cardinality) of the given object currently in the bag.

public Iterator<E>

Returns:

iterator over all elements in the Bag
iterator
()

Redeclares java.util.Collection.iterator.

Returns an Iterator over the entire set of members, including copies due to cardinality.
public boolean

Returns:

true if this call changed the collection
remove
(Object
the object to remove
object
)

Redeclares java.util.Collection.remove.

(Violation) Removes all occurrences of the given object from the bag.
public boolean

Returns:

true if this call changed the collection
remove
(Object
the object to remove
object
,
int
the number of copies to remove
nCopies
)

Removes nCopies copies of the specified object from the Bag.

public boolean

Returns:

true if this call changed the collection
removeAll
(Collection<?>
the collection to remove
coll
)

Redeclares java.util.Collection.removeAll.

(Violation) Remove all elements represented in the given collection, respecting cardinality.
public boolean

Returns:

true if this call changed the collection
retainAll
(Collection<?>
the collection to retain
coll
)

Redeclares java.util.Collection.retainAll.

(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.
public int

Returns:

the total size of the Bag
size
()

Redeclares java.util.Collection.size.

Returns the total number of items in the bag across all types.
public Set<E>

Returns:

the Set of unique Bag elements
uniqueSet
()

Returns a Set of unique elements in the Bag.

Inherited from java.util.Collection:
addAllclearcontainsequalshashCodeisEmptyparallelStreamremoveIfspliteratorstreamtoArraytoArraytoArray

Method Detail

addback to summary
public boolean add(E object)

Redeclares java.util.Collection.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
addback to summary
public boolean add(E object, int nCopies)

Adds nCopies copies 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 nCopies.

Parameters
object:E

the object to add

nCopies:int

the number of copies to add

Returns:boolean

true if the object was not already in the uniqueSet

containsAllback to summary
public boolean containsAll(Collection<?> coll)

Redeclares java.util.Collection.containsAll.

(Violation) Returns true if the bag contains all elements in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object, calling getCount(Object) on that object must be >= n for all n in coll.

The Collection#containsAll(Collection) method specifies that cardinality should not be respected; this method should return true if the bag contains at least one of every object contained in the given collection.

Parameters
coll:Collection<?>

the collection to check against

Returns:boolean

true if the Bag contains all the collection

Annotations
@Override
getCountback to summary
public int getCount(Object object)

Returns the number of occurrences (cardinality) of the given object currently in the bag. If the object does not exist in the bag, return 0.

Parameters
object:Object

the object to search for

Returns:int

the number of occurrences of the object, zero if not found

iteratorback to summary
public Iterator<E> iterator()

Redeclares java.util.Collection.iterator.

Returns an Iterator over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.

Returns:Iterator<E>

iterator over all elements in the Bag

Annotations
@Override
removeback to summary
public boolean remove(Object object)

Redeclares java.util.Collection.remove.

(Violation) Removes all occurrences of the given object from the bag.

This will also remove the object from the uniqueSet().

According to the Collection#remove(Object) method, this method should only remove the first occurrence of the given object, not all occurrences.

Parameters
object:Object

the object to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
removeback to summary
public boolean remove(Object object, int nCopies)

Removes nCopies copies of the specified object from the Bag.

If the number of copies to remove is greater than the actual number of copies in the Bag, no error is thrown.

Parameters
object:Object

the object to remove

nCopies:int

the number of copies to remove

Returns:boolean

true if this call changed the collection

removeAllback to summary
public boolean removeAll(Collection<?> coll)

Redeclares java.util.Collection.removeAll.

(Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object, the bag will have n fewer copies, assuming the bag had at least n copies to begin with.

The Collection#removeAll(Collection) method specifies that cardinality should not be respected; this method should remove all occurrences of every object contained in the given collection.

Parameters
coll:Collection<?>

the collection to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
retainAllback to summary
public boolean retainAll(Collection<?> coll)

Redeclares java.util.Collection.retainAll.

(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object and the bag has m > n copies, then delete m - n copies from the bag. In addition, if e is an object in the bag but !coll.contains(e), then remove e and any of its copies.

The Collection#retainAll(Collection) method specifies that cardinality should not be respected; this method should keep all occurrences of every object contained in the given collection.

Parameters
coll:Collection<?>

the collection to retain

Returns:boolean

true if this call changed the collection

Annotations
@Override
sizeback to summary
public int size()

Redeclares java.util.Collection.size.

Returns the total number of items in the bag across all types.

Returns:int

the total size of the Bag

Annotations
@Override
uniqueSetback to summary
public Set<E> uniqueSet()

Returns a Set of unique elements in the Bag.

Uniqueness constraints are the same as those in java.util.Set.

Returns:Set<E>

the Set of unique Bag elements