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>>

public class ProbabilityMap<E> extends Object implements 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.
  • Constructor Details

    • ProbabilityMap

      public ProbabilityMap()
      Default constructor.
  • Method Details

    • add

      public boolean add(E element, double chance)
      Adds an element with the given weight to the map.
      Parameters:
      element - The element to add.
      chance - The non-negative weight to map to the element.
      Returns:
      true if the element was successfully added.
    • remove

      public boolean remove(E element)
      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:
      true if the element was removed.
    • get

      @Nullable public E get()
      Returns a random element based on each element's weight. If there are no elements in the set, then this method will return null.
      Returns:
      The randomized element.
    • isEmpty

      public boolean isEmpty()
      Returns true if there are no elements added to the map.
      Returns:
      true if 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

      @NotNull public @NotNull Iterator<ProbabilityMap.Node<E>> iterator()
      Specified by:
      iterator in interface Iterable<E>