Class SortedMultiset_TopDown<T extends java.lang.Comparable<T>>

java.lang.Object
java.util.AbstractMap<K,​V>
java.util.TreeMap<T,​java.lang.Integer>
de.tilman_neumann.util.SortedMultiset_TopDown<T>
Type Parameters:
T - value class
All Implemented Interfaces:
Multiset<T>, SortedMultiset<T>, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<SortedMultiset<T>>, java.util.Map<T,​java.lang.Integer>, java.util.NavigableMap<T,​java.lang.Integer>, java.util.SortedMap<T,​java.lang.Integer>
Direct Known Subclasses:
MpiPartition

public class SortedMultiset_TopDown<T extends java.lang.Comparable<T>>
extends java.util.TreeMap<T,​java.lang.Integer>
implements SortedMultiset<T>
A sorted set of elements with multiple occurrences, sorted biggest elements first. Sorting is due to extending TreeMap; with HashMap the sorting behavior would be undefined. Note that TreeMaps demand that all keys inserted into the map must implement the Comparable interface!
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
    SortedMultiset_TopDown()
    Constructor for an empty multiset, sorted biggest elements first.
    SortedMultiset_TopDown​(Multiset<T> original)
    Copy constructor.
    SortedMultiset_TopDown​(java.util.Collection<T> values)
    Constructor from an ordinary collection.
    SortedMultiset_TopDown​(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.
    int compareTo​(SortedMultiset<T> other)
    Compares this to another ComparableMultiset.
    boolean equals​(java.lang.Object o)
    Sorted multisets are equal if they have exactly the same elements and these elements the same multiplicity.
    T getBiggestElement()  
    T getSmallestElement()  
    java.util.Iterator<java.util.Map.Entry<T,​java.lang.Integer>> getTopDownIterator()  
    int hashCode()  
    SortedMultiset<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​(java.lang.String entrySep, java.lang.String expSep)
    Conversion to String, with
    int totalCount()  

    Methods inherited from class java.util.TreeMap

    ceilingEntry, ceilingKey, clear, clone, comparator, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, forEach, get, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, merge, navigableKeySet, pollFirstEntry, pollLastEntry, put, putAll, putIfAbsent, replace, replace, replaceAll, size, subMap, subMap, tailMap, tailMap, values

    Methods inherited from class java.util.AbstractMap

    isEmpty, toString

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface java.util.Map

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

    Methods inherited from interface de.tilman_neumann.util.Multiset

    entrySet, get, keySet, size, toString

    Methods inherited from interface java.util.SortedMap

    comparator, entrySet, firstKey, keySet, lastKey, values
  • Constructor Details

    • SortedMultiset_TopDown

      public SortedMultiset_TopDown()
      Constructor for an empty multiset, sorted biggest elements first. This sort order is particularly adequate for the parts of additive partitions.
    • SortedMultiset_TopDown

      public SortedMultiset_TopDown​(java.util.Collection<T> values)
      Constructor from an ordinary collection. Bigger elements are sorted in before smaller elements.
      Parameters:
      values -
    • SortedMultiset_TopDown

      public SortedMultiset_TopDown​(T[] values)
      Constructor from a value array. Bigger elements are sorted in before smaller elements.
      Parameters:
      values -
    • SortedMultiset_TopDown

      public SortedMultiset_TopDown​(Multiset<T> original)
      Copy constructor. Bigger elements are sorted in before smaller elements.
      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 extends java.lang.Comparable<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 extends java.lang.Comparable<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 extends java.lang.Comparable<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 extends java.lang.Comparable<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 extends java.lang.Comparable<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 extends java.lang.Comparable<T>,​java.lang.Integer>
      Specified by:
      remove in interface Multiset<T extends java.lang.Comparable<T>>
      Overrides:
      remove in class java.util.TreeMap<T extends java.lang.Comparable<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 extends java.lang.Comparable<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 extends java.lang.Comparable<T>>
      Returns:
      previous multiplicity of the argument
    • intersect

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

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

      public T getSmallestElement()
      Specified by:
      getSmallestElement in interface SortedMultiset<T extends java.lang.Comparable<T>>
      Returns:
      The smallest element.
    • getBiggestElement

      public T getBiggestElement()
      Specified by:
      getBiggestElement in interface SortedMultiset<T extends java.lang.Comparable<T>>
      Returns:
      The biggest element.
    • toList

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

      public java.util.Iterator<java.util.Map.Entry<T,​java.lang.Integer>> getTopDownIterator()
      Specified by:
      getTopDownIterator in interface SortedMultiset<T extends java.lang.Comparable<T>>
      Returns:
      an iterator that returns biggest elements first.
    • compareTo

      public int compareTo​(SortedMultiset<T> other)
      Compares this to another ComparableMultiset. A multiset is bigger than another one if it's biggest element is bigger than the largest element of the other multiset, or the 2.nd-biggest if the biggest elements are equal, or the 3.rd biggest, and so on.
      Specified by:
      compareTo in interface java.lang.Comparable<T extends java.lang.Comparable<T>>
      Returns:
      <0/0/>0 if this is smaller than/equal to /bigger than other.
    • equals

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

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

      public java.lang.String toString​(java.lang.String entrySep, java.lang.String expSep)
      Description copied from interface: SortedMultiset
      Conversion to String, with
      Specified by:
      toString in interface SortedMultiset<T extends java.lang.Comparable<T>>
      Parameters:
      entrySep - e.g. "*" for multiplicative elements
      expSep - e.g. "^" for multiplicative elements
      Returns:
      a string representation of this