Package me.deecaad.core.utils
Class ProbabilityMap<E>
java.lang.Object
me.deecaad.core.utils.ProbabilityMap<E>
- Type Parameters:
E- The type of the element to store.
- All Implemented Interfaces:
Iterable<ProbabilityMap.Node<E>>
This class outlines a mapping of elements to a weight. This data structure allows real time
getting of random elements with weight.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds anelementwith the given weight to the map.get()Returns a random element based on each element's weight.booleanisEmpty()Returnstrueif there are no elements added to the map.@NotNull Iterator<ProbabilityMap.Node<E>> iterator()booleanRemoves the given element, if it is present in the map.intsize()Returns the number of elements in the map.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ProbabilityMap
public ProbabilityMap()Default constructor.
-
-
Method Details
-
add
Adds anelementwith the given weight to the map.- Parameters:
element- The element to add.chance- The non-negative weight to map to the element.- Returns:
trueif the element was successfully added.
-
remove
Removes the given element, if it is present in the map. This method has an O notation of O(n) in both best and worst case scenarios.- Parameters:
element- The element to remove.- Returns:
trueif the element was removed.
-
get
Returns a random element based on each element's weight. If there are no elements in the set, then this method will returnnull.- Returns:
- The randomized element.
-
isEmpty
public boolean isEmpty()Returnstrueif there are no elements added to the map.- Returns:
trueif the backing map is empty.
-
size
public int size()Returns the number of elements in the map.- Returns:
- The amount of elements in the map.
-
iterator
-