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:
- Iterable<E>,- Collection<E>,- Bag<E>
Abstract implementation of the 
Bag interface 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)
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected static classMutable integer class for storing the data.
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedConstructor needed for subclass serialization.protectedConstructor that assigns the specified Map as the backing store.protectedAbstractMapBag(Map<E, AbstractMapBag.MutableInteger> map, Iterable<? extends E> iterable) Constructs a new instance that assigns the specified Map as the backing store.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanAdds a new element to the bag, incrementing its count in the underlying map.booleanAdds a new element to the bag, incrementing its count in the map.booleanaddAll(Collection<? extends E> coll) Invokesadd(Object)for each element in the given collection.voidclear()Clears the bag by clearing the underlying map.booleanDetermines if the bag contains the given element by checking if the underlying map contains the element as a key.booleancontainsAll(Collection<?> coll) Determines if the bag contains the given elements.protected voidReads the map in using a custom routine.protected voidWrites the map out using a custom routine.booleanCompares this Bag to another.intGets the number of occurrence of the given element in this bag by looking up its count in the underlying map.protected Map<E, AbstractMapBag.MutableInteger> getMap()Utility method for implementations to access the map that backs this bag.inthashCode()Gets a hash code for the Bag compatible with the definition of equals.booleanisEmpty()Returns true if the underlying map is empty.iterator()Gets an iterator over the bag elements.booleanRemoves all copies of the specified object from the bag.booleanRemoves a specified number of copies of an object from the bag.booleanremoveAll(Collection<?> coll) Removes objects from the bag according to their count in the specified collection.booleanretainAll(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.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.toString()Implement a toString() method suitable for debugging.Returns an unmodifiable view of the underlying map's key set.Methods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.CollectionparallelStream, removeIf, spliterator, stream
- 
Constructor Details- 
AbstractMapBagprotected AbstractMapBag()Constructor needed for subclass serialization.
- 
AbstractMapBagConstructor that assigns the specified Map as the backing store. The map must be empty and non-null.- Parameters:
- map- the map to assign
 
- 
AbstractMapBagConstructs a new instance that assigns the specified Map as the backing store. The map must be empty and non-null. The bag is filled from the iterable elements.- Parameters:
- map- the map to assign.
- iterable- The bag is filled from these iterable elements.
 
 
- 
- 
Method Details- 
add
- 
add
- 
addAllInvokesadd(Object)for each element in the given collection.- Specified by:
- addAllin interface- Collection<E>
- Parameters:
- coll- the collection to add
- Returns:
- trueif this call changed the bag
 
- 
clearClears the bag by clearing the underlying map.- Specified by:
- clearin interface- Collection<E>
 
- 
containsDetermines if the bag contains the given element by checking if the underlying map contains the element as a key.- Specified by:
- containsin interface- Collection<E>
- Parameters:
- object- the object to search for
- Returns:
- true if the bag contains the given element
 
- 
containsAllDetermines if the bag contains the given elements.- Specified by:
- containsAllin interface- Bag<E>
- Specified by:
- containsAllin interface- Collection<E>
- Parameters:
- coll- the collection to check against
- Returns:
- trueif the Bag contains all the collection
 
- 
doReadObjectprotected void doReadObject(Map<E, AbstractMapBag.MutableInteger> map, ObjectInputStream in) throws IOException, ClassNotFoundExceptionReads the map in using a custom routine.- Parameters:
- map- the map to use
- in- the input stream
- Throws:
- IOException- any of the usual I/O related exceptions
- ClassNotFoundException- if the stream contains an object which class cannot be loaded
- ClassCastException- if the stream does not contain the correct objects
 
- 
doWriteObjectWrites the map out using a custom routine.- Parameters:
- out- the output stream
- Throws:
- IOException- any of the usual I/O related exceptions
 
- 
equalsCompares this Bag to another. This Bag equals another Bag if it contains the same number of occurrences of the same elements.- Specified by:
- equalsin interface- Collection<E>
- Overrides:
- equalsin class- Object
- Parameters:
- object- the Bag to compare to
- Returns:
- true if equal
 
- 
getCountGets the number of occurrence of the given element in this bag by looking up its count in the underlying map.
- 
getMapUtility method for implementations to access the map that backs this bag. Not intended for interactive use outside of subclasses.- Returns:
- the map being used by the Bag
 
- 
hashCodeGets 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()) ^ noOccurrences). This hash code is compatible with the Set interface.- Specified by:
- hashCodein interface- Collection<E>
- Overrides:
- hashCodein class- Object
- Returns:
- the hash code of the Bag
 
- 
isEmptyReturns true if the underlying map is empty.- Specified by:
- isEmptyin interface- Collection<E>
- Returns:
- true if bag is empty
 
- 
iteratorGets an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly.
- 
remove
- 
remove
- 
removeAllRemoves objects from the bag according to their count in the specified collection.
- 
retainAllRemove any members of the bag that are not in the given bag, respecting cardinality.
- 
size
- 
toArrayReturns an array of all of this bag's elements.- Specified by:
- toArrayin interface- Collection<E>
- Returns:
- an array of all of this bag's elements
 
- 
toArrayReturns 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 interface- 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:
- ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of the elements in this list
- NullPointerException- if the specified array is null
 
- 
toString
- 
uniqueSet
 
-