Class AbstractMapBag<E>
- java.lang.Object
 - 
- org.apache.commons.collections4.bag.AbstractMapBag<E>
 
 
- 
- Type Parameters:
 E- the type of elements in this bag
- All Implemented Interfaces:
 java.lang.Iterable<E>,java.util.Collection<E>,Bag<E>
public abstract class AbstractMapBag<E> extends java.lang.Object implements Bag<E>
Abstract implementation of theBaginterface to simplify the creation of subclass implementations.Subclasses specify a Map implementation to use as the internal storage. The map will be used to map bag elements to a number; the number represents the number of occurrences of that element in the bag.
- Since:
 - 3.0 (previously DefaultMapBag v2.0)
 
 
- 
- 
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E object)Adds a new element to the bag, incrementing its count in the underlying map.booleanadd(E object, int nCopies)Adds a new element to the bag, incrementing its count in the map.booleanaddAll(java.util.Collection<? extends E> coll)Invokesadd(Object)for each element in the given collection.voidclear()Clears the bag by clearing the underlying map.booleancontains(java.lang.Object object)Determines if the bag contains the given element by checking if the underlying map contains the element as a key.booleancontainsAll(java.util.Collection<?> coll)Determines if the bag contains the given elements.booleanequals(java.lang.Object object)Compares this Bag to another.intgetCount(java.lang.Object object)Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.inthashCode()Gets a hash code for the Bag compatible with the definition of equals.booleanisEmpty()Returns true if the underlying map is empty.java.util.Iterator<E>iterator()Gets an iterator over the bag elements.booleanremove(java.lang.Object object)Removes all copies of the specified object from the bag.booleanremove(java.lang.Object object, int nCopies)Removes a specified number of copies of an object from the bag.booleanremoveAll(java.util.Collection<?> coll)Removes objects from the bag according to their count in the specified collection.booleanretainAll(java.util.Collection<?> coll)Remove any members of the bag that are not in the given bag, respecting cardinality.intsize()Returns the number of elements in this bag.java.lang.Object[]toArray()Returns an array of all of this bag's elements.<T> T[]toArray(T[] array)Returns an array of all of this bag's elements.java.lang.StringtoString()Implement a toString() method suitable for debugging.java.util.Set<E>uniqueSet()Returns an unmodifiable view of the underlying map's key set. 
 - 
 
- 
- 
Method Detail
- 
size
public int size()
Returns the number of elements in this bag. 
- 
isEmpty
public boolean isEmpty()
Returns true if the underlying map is empty.- Specified by:
 isEmptyin interfacejava.util.Collection<E>- Returns:
 - true if bag is empty
 
 
- 
getCount
public int getCount(java.lang.Object object)
Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map. 
- 
contains
public boolean contains(java.lang.Object object)
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.- Specified by:
 containsin interfacejava.util.Collection<E>- Parameters:
 object- the object to search for- Returns:
 - true if the bag contains the given element
 
 
- 
containsAll
public boolean containsAll(java.util.Collection<?> coll)
Determines if the bag contains the given elements.- Specified by:
 containsAllin interfaceBag<E>- Specified by:
 containsAllin interfacejava.util.Collection<E>- Parameters:
 coll- the collection to check against- Returns:
 trueif the Bag contains all the collection
 
- 
iterator
public java.util.Iterator<E> iterator()
Gets an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly. 
- 
add
public boolean add(E object)
Adds a new element to the bag, incrementing its count in the underlying map. 
- 
add
public boolean add(E object, int nCopies)
Adds a new element to the bag, incrementing its count in the map. 
- 
addAll
public boolean addAll(java.util.Collection<? extends E> coll)
Invokesadd(Object)for each element in the given collection.- Specified by:
 addAllin interfacejava.util.Collection<E>- Parameters:
 coll- the collection to add- Returns:
 trueif this call changed the bag
 
- 
clear
public void clear()
Clears the bag by clearing the underlying map.- Specified by:
 clearin interfacejava.util.Collection<E>
 
- 
remove
public boolean remove(java.lang.Object object)
Removes all copies of the specified object from the bag. 
- 
remove
public boolean remove(java.lang.Object object, int nCopies)Removes a specified number of copies of an object from the bag. 
- 
removeAll
public boolean removeAll(java.util.Collection<?> coll)
Removes objects from the bag according to their count in the specified collection. 
- 
retainAll
public boolean retainAll(java.util.Collection<?> coll)
Remove any members of the bag that are not in the given bag, respecting cardinality. 
- 
toArray
public java.lang.Object[] toArray()
Returns an array of all of this bag's elements.- Specified by:
 toArrayin interfacejava.util.Collection<E>- Returns:
 - an array of all of this bag's elements
 
 
- 
toArray
public <T> T[] toArray(T[] array)
Returns an array of all of this bag's elements. If the input array has more elements than are in the bag, trailing elements will be set to null.- Specified by:
 toArrayin interfacejava.util.Collection<E>- Type Parameters:
 T- the type of the array elements- Parameters:
 array- the array to populate- Returns:
 - an array of all of this bag's elements
 - Throws:
 java.lang.ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of the elements in this listjava.lang.NullPointerException- if the specified array is null
 
- 
uniqueSet
public java.util.Set<E> uniqueSet()
Returns an unmodifiable view of the underlying map's key set. 
- 
equals
public boolean equals(java.lang.Object object)
Compares this Bag to another. This Bag equals another Bag if it contains the same number of occurrences of the same elements.- Specified by:
 equalsin interfacejava.util.Collection<E>- Overrides:
 equalsin classjava.lang.Object- Parameters:
 object- the Bag to compare to- Returns:
 - true if equal
 
 
- 
hashCode
public int hashCode()
Gets a hash code for the Bag compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as(e==null ? 0 : e.hashCode()) ^ noOccurances). This hash code is compatible with the Set interface.- Specified by:
 hashCodein interfacejava.util.Collection<E>- Overrides:
 hashCodein classjava.lang.Object- Returns:
 - the hash code of the Bag
 
 
- 
toString
public java.lang.String toString()
Implement a toString() method suitable for debugging.- Overrides:
 toStringin classjava.lang.Object- Returns:
 - a debugging toString
 
 
 - 
 
 -