Package de.tilman_neumann.util
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 wasjava.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
-
Constructor Details
-
Multiset_HashMapImpl
public Multiset_HashMapImpl()Constructor for an empty multiset. -
Multiset_HashMapImpl
Constructor from an ordinary collection.- Parameters:
values
-
-
Multiset_HashMapImpl
Constructor from a value array.- Parameters:
values
-
-
Multiset_HashMapImpl
Copy constructor.- Parameters:
original
-
-
-
Method Details
-
add
Description copied from interface:Multiset
Add an entry with multiplicity 1. -
add
Description copied from interface:Multiset
Add one entry with given multiplicity. -
addAll
Description copied from interface:Multiset
Add another multiset to this. -
addAll
Description copied from interface:Multiset
Add all values of the given collection. -
addAll
Description copied from interface:Multiset
Add all values of the given array. -
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. -
remove
Description copied from interface:Multiset
Remove the given key multiple times. -
removeAll
Description copied from interface:Multiset
Removes the key-value pair of the given key no matter what its multiplicity was -
intersect
Description copied from interface:Multiset
Returns the multiset of elements contained in both this and in the other multiset. -
totalCount
public int totalCount()- Specified by:
totalCount
in interfaceMultiset<T>
- Returns:
- Total number of elements, i.e. the sum of multiplicities of all different elements
-
toList
-
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 "^". -
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. -
hashCode
public int hashCode()
-