common
Class HashMultiset

java.lang.Object
  extended by java.util.AbstractCollection
      extended by common.AbstractMultiset
          extended by common.HashMultiset
All Implemented Interfaces:
Multiset, java.lang.Iterable, java.util.Collection

public class HashMultiset
extends AbstractMultiset

An implementation of the Multiset interface that uses a HashMap from elements to integers as the underlying data structure.


Constructor Summary
HashMultiset()
          Creates an empty HashMultiset.
HashMultiset(java.util.Collection c)
          Creates a HashMultiset with the same multiset of elements as the given Collection.
 
Method Summary
 boolean add(java.lang.Object o)
          Adds a single copy of the given element to this multiset.
 void clear()
          Removes all elements from this multiset.
 int count(java.lang.Object o)
          Returns the number of occurrences of the given element in this multiset.
 java.util.Set entrySet()
          Returns the set of entries in the multiset.
 java.util.Iterator iterator()
          Returns an iterator over this multiset.
static void main(java.lang.String[] args)
          Test program
 boolean remove(java.lang.Object o)
          Removes a single copy of the given element from this multiset, if such an element is present.
 int size()
          Returns the sum of the occurrence counts of all items.
 
Methods inherited from class common.AbstractMultiset
contains, equals, hashCode
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

HashMultiset

public HashMultiset()
Creates an empty HashMultiset.


HashMultiset

public HashMultiset(java.util.Collection c)
Creates a HashMultiset with the same multiset of elements as the given Collection.

Method Detail

size

public int size()
Returns the sum of the occurrence counts of all items.

Specified by:
size in interface java.util.Collection
Specified by:
size in class java.util.AbstractCollection

count

public int count(java.lang.Object o)
Returns the number of occurrences of the given element in this multiset.

Specified by:
count in interface Multiset
Specified by:
count in class AbstractMultiset

add

public boolean add(java.lang.Object o)
Adds a single copy of the given element to this multiset.

Specified by:
add in interface java.util.Collection
Overrides:
add in class java.util.AbstractCollection
Returns:
true in all cases

remove

public boolean remove(java.lang.Object o)
Removes a single copy of the given element from this multiset, if such an element is present.

Specified by:
remove in interface java.util.Collection
Overrides:
remove in class java.util.AbstractCollection
Returns:
true if the given element was present

clear

public void clear()
Removes all elements from this multiset.

Specified by:
clear in interface java.util.Collection
Overrides:
clear in class java.util.AbstractCollection

iterator

public java.util.Iterator iterator()
Returns an iterator over this multiset. If the multiset contains n copies of an element, the iterator will return that element n times.

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in class java.util.AbstractCollection

entrySet

public java.util.Set entrySet()
Returns the set of entries in the multiset. An entry is a pair (e, n) where e is an element of the multiset and n is the number of times e occurs in the multiset. The returned set contains exactly one entry for each distinct element e. Thus, entrySet.size() returns the number of distinct elements in the multiset.

Specified by:
entrySet in interface Multiset
Specified by:
entrySet in class AbstractMultiset
Returns:
a Set of Multiset.Entry objects

main

public static void main(java.lang.String[] args)
Test program