Class Multiset_HashMapImpl<T>

java.lang.Object
java.util.AbstractMap<K,​V>
java.util.HashMap<T,​java.lang.Integer>
de.tilman_neumann.util.Multiset_HashMapImpl<T>
Type Parameters:
T - element class, must have consistent equals() and hashCode() methods
All Implemented Interfaces:
Multiset<T>, java.io.Serializable, java.lang.Cloneable, java.util.Map<T,​java.lang.Integer>

public class Multiset_HashMapImpl<T>
extends java.util.HashMap<T,​java.lang.Integer>
implements Multiset<T>
A set of unsorted elements with multiple occurences. This is an exact implementation of multisets: - Two multisets like {a, a, b} and {a, b, a} are equal - and there is no ordering relation between elements. As such, the implementation can be based on HashMaps which also have no defined sort order. This has the following consequences: - HashMaps are usually faster than SortedMaps - without defined sort order, it makes no sense to demand entries to be Comparable, neither for the multisets themselves because an ordering of the multisets would depend on one for their entries. - output of these multisets will usually look a bit ugly
See Also:
Serialized Form
  • Nested Class Summary

    Nested classes/interfaces inherited from class java.util.AbstractMap

    java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>

    Nested classes/interfaces inherited from interface java.util.Map

    java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
  • Constructor Summary

    Constructors
    Constructor Description
    Multiset_HashMapImpl()
    Constructor for an empty multiset.
    Multiset_HashMapImpl​(Multiset<T> original)
    Copy constructor.
    Multiset_HashMapImpl​(java.util.Collection<T> values)
    Constructor from an ordinary collection.
    Multiset_HashMapImpl​(T[] values)
    Constructor from a value array.
  • Method Summary

    Modifier and Type Method Description
    int add​(T entry)
    Add an entry with multiplicity 1.
    int add​(T entry, int mult)
    Add one entry with given multiplicity.
    void addAll​(Multiset<T> other)
    Add another multiset to this.
    void addAll​(java.util.Collection<T> values)
    Add all values of the given collection.
    void addAll​(T[] values)
    Add all values of the given array.
    boolean equals​(java.lang.Object o)
    Unordered multisets are equal if they have exactly the same elements and these elements the same multiplicity, no matter in which iteration order the elements appear.
    int hashCode()  
    Multiset<T> intersect​(Multiset<T> other)
    Returns the multiset of elements contained in both this and in the other multiset.
    java.lang.Integer remove​(java.lang.Object key)
    Removes one instance of the given value from this multiset, if at least one element is contained.
    int remove​(T key, int mult)
    Remove the given key multiple times.
    int removeAll​(T key)
    Removes the key-value pair of the given key no matter what its multiplicity was
    java.util.List<T> toList()  
    java.lang.String toString()
    Returns a string representation of the unsorted multiset similar to collections, with distinct keys separated by commas and the multiplicity indicated by "^".
    int totalCount()  

    Methods inherited from class java.util.HashMap

    clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, replace, replace, replaceAll, size, values

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface de.tilman_neumann.util.Multiset

    entrySet, get, keySet, size
  • Constructor Details

    • Multiset_HashMapImpl

      public Multiset_HashMapImpl()
      Constructor for an empty multiset.
    • Multiset_HashMapImpl

      public Multiset_HashMapImpl​(java.util.Collection<T> values)
      Constructor from an ordinary collection.
      Parameters:
      values -
    • Multiset_HashMapImpl

      public Multiset_HashMapImpl​(T[] values)
      Constructor from a value array.
      Parameters:
      values -
    • Multiset_HashMapImpl

      public Multiset_HashMapImpl​(Multiset<T> original)
      Copy constructor.
      Parameters:
      original -
  • Method Details

    • add

      public int add​(T entry)
      Description copied from interface: Multiset
      Add an entry with multiplicity 1.
      Specified by:
      add in interface Multiset<T>
      Returns:
      the previous multiplicity of the entry
    • add

      public int add​(T entry, int mult)
      Description copied from interface: Multiset
      Add one entry with given multiplicity.
      Specified by:
      add in interface Multiset<T>
      Returns:
      the previous multiplicity of the entry
    • addAll

      public void addAll​(Multiset<T> other)
      Description copied from interface: Multiset
      Add another multiset to this.
      Specified by:
      addAll in interface Multiset<T>
    • addAll

      public void addAll​(java.util.Collection<T> values)
      Description copied from interface: Multiset
      Add all values of the given collection.
      Specified by:
      addAll in interface Multiset<T>
    • addAll

      public void addAll​(T[] values)
      Description copied from interface: Multiset
      Add all values of the given array.
      Specified by:
      addAll in interface Multiset<T>
    • remove

      public java.lang.Integer remove​(java.lang.Object key)
      Description copied from interface: Multiset
      Removes one instance of the given value from this multiset, if at least one element is contained. The key is declared as Object and not as T to match exactly the signature of the same method in the Map-interface. Nevertheless, of course the arguments should be of type T.
      Specified by:
      remove in interface java.util.Map<T,​java.lang.Integer>
      Specified by:
      remove in interface Multiset<T>
      Overrides:
      remove in class java.util.HashMap<T,​java.lang.Integer>
      Returns:
      previous multiplicity of the argument
    • remove

      public int remove​(T key, int mult)
      Description copied from interface: Multiset
      Remove the given key multiple times.
      Specified by:
      remove in interface Multiset<T>
      Returns:
      old multiplicity
    • removeAll

      public int removeAll​(T key)
      Description copied from interface: Multiset
      Removes the key-value pair of the given key no matter what its multiplicity was
      Specified by:
      removeAll in interface Multiset<T>
      Returns:
      previous multiplicity of the argument
    • intersect

      public Multiset<T> intersect​(Multiset<T> other)
      Description copied from interface: Multiset
      Returns the multiset of elements contained in both this and in the other multiset.
      Specified by:
      intersect in interface Multiset<T>
      Returns:
      intersection of this and other
    • totalCount

      public int totalCount()
      Specified by:
      totalCount in interface Multiset<T>
      Returns:
      Total number of elements, i.e. the sum of multiplicities of all different elements
    • toList

      public java.util.List<T> toList()
      Specified by:
      toList in interface Multiset<T>
      Returns:
      this as a flat list in which each value occurs 'multiplicity' times.
    • toString

      public java.lang.String toString()
      Returns a string representation of the unsorted multiset similar to collections, with distinct keys separated by commas and the multiplicity indicated by "^".
      Specified by:
      toString in interface Multiset<T>
      Overrides:
      toString in class java.util.AbstractMap<T,​java.lang.Integer>
    • equals

      public boolean equals​(java.lang.Object o)
      Unordered multisets are equal if they have exactly the same elements and these elements the same multiplicity, no matter in which iteration order the elements appear.
      Specified by:
      equals in interface java.util.Map<T,​java.lang.Integer>
      Specified by:
      equals in interface Multiset<T>
      Overrides:
      equals in class java.util.AbstractMap<T,​java.lang.Integer>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface java.util.Map<T,​java.lang.Integer>
      Overrides:
      hashCode in class java.util.AbstractMap<T,​java.lang.Integer>