com.google.common.collect
Class HashMultiset<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by com.google.common.collect.AbstractMultiset<E>
          extended by com.google.common.collect.HashMultiset<E>
All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>

public final class HashMultiset<E>
extends AbstractMultiset<E>

Multiset implementation backed by a HashMap.

Author:
Kevin Bourrillion
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
 
Constructor Summary
HashMultiset()
          Constructs a new empty HashMultiset using the default initial capacity (16 distinct elements) and load factor (0.75).
HashMultiset(int distinctElements)
          Constructs a new empty HashMultiset with the specified expected number of distinct elements and the default load factor (0.75).
HashMultiset(int initialCapacity, float loadFactor)
          Constructs a new empty HashMultiset using the specified initial capacity (distinct elements) and load factor.
HashMultiset(Iterable<? extends E> elements)
          Constructs a new HashMultiset containing the specified elements.
HashMultiset(Multiset<? extends E> elements)
          Constructs a new HashMultiset containing the specified elements.
 
Method Summary
 boolean add(E element, int occurrences)
          Adds a number of occurrences of the specified element to this multiset.
protected  Map<E,AtomicInteger> backingMap()
           
 int count(Object element)
          Returns the number of occurrences of the specified element in this multiset.
protected  Set<E> createElementSet()
          Creates a new instance of this multiset's element set, which will be returned by AbstractMultiset.elementSet.
 Set<Multiset.Entry<E>> entrySet()
          Returns the data of this multiset as a set of Entry instances.
 Iterator<E> iterator()
          
 int remove(Object element, int occurrences)
          Removes a number of occurrences of the specified element from this multiset.
 int removeAllOccurrences(Object element)
          Removes all occurrences of the specified element from this multiset.
 int size()
          
 
Methods inherited from class com.google.common.collect.AbstractMultiset
add, addAll, clear, contains, containsAll, elementSet, equals, hashCode, isEmpty, remove, removeAll, retainAll, toString
 
Methods inherited from class java.util.AbstractCollection
toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
toArray, toArray
 

Constructor Detail

HashMultiset

public HashMultiset()
Constructs a new empty HashMultiset using the default initial capacity (16 distinct elements) and load factor (0.75).


HashMultiset

public HashMultiset(int distinctElements)
Constructs a new empty HashMultiset with the specified expected number of distinct elements and the default load factor (0.75).

Parameters:
distinctElements - the expected number of distinct elements
Throws:
IllegalArgumentException - if distinctElements is negative

HashMultiset

public HashMultiset(int initialCapacity,
                    float loadFactor)
Constructs a new empty HashMultiset using the specified initial capacity (distinct elements) and load factor.

Parameters:
initialCapacity - the initial capacity
loadFactor - the load factor
Throws:
IllegalArgumentException - if the initial capacity is negative

HashMultiset

public HashMultiset(Iterable<? extends E> elements)
Constructs a new HashMultiset containing the specified elements. If the specified elements is a Multiset instance, this constructor behaves identically to HashMultiset(Multiset). Otherwise, the default initial capacity (16 distinct elements) and load factor (0.75) is used.

Parameters:
elements - the elements that the multiset should contain

HashMultiset

public HashMultiset(Multiset<? extends E> elements)
Constructs a new HashMultiset containing the specified elements. The multiset is created with the default load factor (0.75) and an initial capacity sufficient to hold the specified elements.

Parameters:
elements - the elements that the multiset should contain
Method Detail

backingMap

protected Map<E,AtomicInteger> backingMap()

entrySet

public Set<Multiset.Entry<E>> entrySet()
Returns the data of this multiset as a set of Entry instances. This set contains precisely one Entry instance for each distinct element of the multiset. The iteration order of this set is implementation-dependent.

Invoking Multiset.Entry.getCount() on an entry in the returned set always returns the current count of that element in the multiset, as opposed to the count at the time the entry was retrieved.

Specified by:
entrySet in interface Multiset<E>
Specified by:
entrySet in class AbstractMultiset<E>
Returns:
the entries of this multiset, viewed as a set

size

public int size()
Description copied from class: AbstractMultiset

This implementation iterates across Multiset.entrySet() and sums the counts of the entries.

Specified by:
size in interface Collection<E>
Overrides:
size in class AbstractMultiset<E>

iterator

public Iterator<E> iterator()
Description copied from class: AbstractMultiset

This implementation usually invokes methods of the Multiset.entrySet() iterator. As the only exception, the iterator's remove method sometimes calls the multiset's remove.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Overrides:
iterator in class AbstractMultiset<E>

count

public int count(@Nullable
                 Object element)
Description copied from class: AbstractMultiset
Returns the number of occurrences of the specified element in this multiset.

This implementation iterates across Multiset.entrySet() and sums the count of all entries.

Specified by:
count in interface Multiset<E>
Overrides:
count in class AbstractMultiset<E>
Parameters:
element - the element to look for
Returns:
the nonnegative number of occurrences of the element
See Also:
Iterables.frequency(java.lang.Iterable, java.lang.Object)

add

public boolean add(@Nullable
                   E element,
                   int occurrences)
Adds a number of occurrences of the specified element to this multiset.

This implementation always throws an UnsupportedOperationException. To support adding elements, override it.

Specified by:
add in interface Multiset<E>
Overrides:
add in class AbstractMultiset<E>
Parameters:
element - the element to add
occurrences - the number of occurrences to add
Returns:
true if the collection changed as a result (this should always be the case unless occurrences is zero)
Throws:
IllegalArgumentException - if the call would result in more than Integer.MAX_VALUE occurrences of element in this multiset.

remove

public int remove(@Nullable
                  Object element,
                  int occurrences)
Description copied from class: AbstractMultiset
Removes a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed.

This implementation always throws an UnsupportedOperationException. To support removing elements, override it.

Specified by:
remove in interface Multiset<E>
Overrides:
remove in class AbstractMultiset<E>
Parameters:
element - the element whose occurrences should be removed
occurrences - the number of occurrences of this element to remove
Returns:
the number of occurrences that were successfully removed (zero if the element was not present)

removeAllOccurrences

public int removeAllOccurrences(@Nullable
                                Object element)
Description copied from class: AbstractMultiset
Removes all occurrences of the specified element from this multiset. This method complements Multiset.remove(java.lang.Object, int), which removes only one occurrence at a time.

This implementation calls AbstractMultiset.remove(Object, int) with Integer.MAX_VALUE occurrences.

Specified by:
removeAllOccurrences in interface Multiset<E>
Overrides:
removeAllOccurrences in class AbstractMultiset<E>
Parameters:
element - the element whose occurrences should all be removed
Returns:
the number of occurrences successfully removed, possibly zero

createElementSet

protected Set<E> createElementSet()
Description copied from class: AbstractMultiset
Creates a new instance of this multiset's element set, which will be returned by AbstractMultiset.elementSet.

Overrides:
createElementSet in class AbstractMultiset<E>