Interface Bag
-
- All Superinterfaces:
java.util.Collection,java.lang.Iterable
- All Known Subinterfaces:
SortedBag
- All Known Implementing Classes:
AbstractBagDecorator,AbstractMapBag,AbstractSortedBagDecorator,DefaultMapBag,HashBag,HashBag,PredicatedBag,PredicatedSortedBag,SynchronizedBag,SynchronizedSortedBag,TransformedBag,TransformedSortedBag,TreeBag,TreeBag,UnmodifiableBag,UnmodifiableSortedBag
public interface Bag extends java.util.CollectionDefines 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}. CallinggetCount(Object)onawould return 2, while callinguniqueSet()would return{a, b, c}.NOTE: This interface violates the
Collectioncontract. The behavior specified in many of these methods is not the same as the behavior specified byCollection. The noncompliant methods are clearly marked with "(Violation)". Exercise caution when using a bag as aCollection.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:
- Commons Collections 2.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanadd(java.lang.Object object)(Violation) Adds one copy the specified object to the Bag.booleanadd(java.lang.Object object, int nCopies)AddsnCopiescopies of the specified object to the Bag.booleancontainsAll(java.util.Collection coll)(Violation) Returnstrueif the bag contains all elements in the given collection, respecting cardinality.intgetCount(java.lang.Object object)Returns the number of occurrences (cardinality) of the given object currently in the bag.java.util.Iteratoriterator()Returns anIteratorover the entire set of members, including copies due to cardinality.booleanremove(java.lang.Object object)(Violation) Removes all occurrences of the given object from the bag.booleanremove(java.lang.Object object, int nCopies)RemovesnCopiescopies of the specified object from the Bag.booleanremoveAll(java.util.Collection coll)(Violation) Remove all elements represented in the given collection, respecting cardinality.booleanretainAll(java.util.Collection coll)(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.intsize()Returns the total number of items in the bag across all types.java.util.SetuniqueSet()Returns aSetof unique elements in the Bag.
-
-
-
Method Detail
-
getCount
int getCount(java.lang.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- the object to search for- Returns:
- the number of occurrences of the object, zero if not found
-
add
boolean add(java.lang.Object object)
(Violation) Adds one copy the specified object to the Bag.If the object is already in the
uniqueSet()then increment its count as reported bygetCount(Object). Otherwise add it to theuniqueSet()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 returntrue. Since it sometimes returnsfalse, this method violates the contract.- Specified by:
addin interfacejava.util.Collection- Parameters:
object- the object to add- Returns:
trueif the object was not already in theuniqueSet
-
add
boolean add(java.lang.Object object, int nCopies)AddsnCopiescopies of the specified object to the Bag.If the object is already in the
uniqueSet()then increment its count as reported bygetCount(Object). Otherwise add it to theuniqueSet()and report its count asnCopies.- Parameters:
object- the object to addnCopies- the number of copies to add- Returns:
trueif the object was not already in theuniqueSet
-
remove
boolean remove(java.lang.Object object)
(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.- Specified by:
removein interfacejava.util.Collection- Returns:
trueif this call changed the collection
-
remove
boolean remove(java.lang.Object object, int nCopies)RemovesnCopiescopies 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- the object to removenCopies- the number of copies to remove- Returns:
trueif this call changed the collection
-
uniqueSet
java.util.Set uniqueSet()
Returns aSetof unique elements in the Bag.Uniqueness constraints are the same as those in
Set.- Returns:
- the Set of unique Bag elements
-
size
int size()
Returns the total number of items in the bag across all types.- Specified by:
sizein interfacejava.util.Collection- Returns:
- the total size of the Bag
-
containsAll
boolean containsAll(java.util.Collection coll)
(Violation) Returnstrueif the bag contains all elements in the given collection, respecting cardinality. That is, if the given collectioncollcontainsncopies of a given object, callinggetCount(Object)on that object must be>= nfor allnincoll.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.- Specified by:
containsAllin interfacejava.util.Collection- Parameters:
coll- the collection to check against- Returns:
trueif the Bag contains all the collection
-
removeAll
boolean removeAll(java.util.Collection coll)
(Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collectioncollcontainsncopies of a given object, the bag will havenfewer copies, assuming the bag had at leastncopies 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.- Specified by:
removeAllin interfacejava.util.Collection- Parameters:
coll- the collection to remove- Returns:
trueif this call changed the collection
-
retainAll
boolean retainAll(java.util.Collection coll)
(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collectioncollcontainsncopies of a given object and the bag hasm > ncopies, then deletem - ncopies from the bag. In addition, ifeis an object in the bag but!coll.contains(e), then removeeand 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.- Specified by:
retainAllin interfacejava.util.Collection- Parameters:
coll- the collection to retain- Returns:
trueif this call changed the collection
-
iterator
java.util.Iterator iterator()
Returns anIteratorover the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Returns:
- iterator over all elements in the Bag
-
-