Package de.tilman_neumann.util
Class Multiset_HashMapImpl<T>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<T,Integer>
-
- de.tilman_neumann.util.Multiset_HashMapImpl<T>
-
- Type Parameters:
T
- element class, must have consistent equals() and hashCode() methods
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<T,Integer>
,Multiset<T>
public class Multiset_HashMapImpl<T> extends HashMap<T,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- Author:
- Tilman Neumann
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description Multiset_HashMapImpl()
Constructor for an empty multiset.Multiset_HashMapImpl(Collection<T> values)
Constructor from an ordinary collection.Multiset_HashMapImpl(Multiset<T> original)
Copy constructor.Multiset_HashMapImpl(T[] values)
Constructor from a value array.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
add(T entry)
int
add(T entry, int mult)
void
addAll(Collection<T> values)
void
addAll(Multiset<T> other)
void
addAll(T[] values)
boolean
equals(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)
Integer
remove(Object key)
int
remove(T key, int mult)
int
removeAll(T key)
List<T>
toList()
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
-
-
-
-
Constructor Detail
-
Multiset_HashMapImpl
public Multiset_HashMapImpl()
Constructor for an empty multiset.
-
Multiset_HashMapImpl
public Multiset_HashMapImpl(Collection<T> values)
Constructor from an ordinary collection.- Parameters:
values
-
-
Multiset_HashMapImpl
public Multiset_HashMapImpl(T[] values)
Constructor from a value array.- Parameters:
values
-
-
-
Method Detail
-
addAll
public void addAll(Collection<T> values)
-
totalCount
public int totalCount()
- Specified by:
totalCount
in interfaceMultiset<T>
-
toString
public 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(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.
-
-