Class WeightedMap<T>

java.lang.Object
com.hypixel.hytale.builtin.hytalegenerator.WeightedMap<T>

public class WeightedMap<T> extends Object
Facilitates picking random values based on weights. The elements' weight proportional to the sum of all weights determines its chance to be picked.

This implementation makes use of the Object::equals(...) and Object::hash() methods.

  • Constructor Details

    • WeightedMap

      public WeightedMap(@Nonnull WeightedMap<T> other)
      Creates a new map that clones over the state and contents of the other provided.
      Parameters:
      other - map to clone from
    • WeightedMap

      public WeightedMap()
      Default constructor.
    • WeightedMap

      public WeightedMap(int initialCapacity)
      Initial expected capacity of the map, not maximum size.
  • Method Details

    • add

      @Nonnull public WeightedMap<T> add(@Nonnull T element, double weight)
      Adds an element and it's weight.
    • get

      public double get(@Nonnull T element)
      Gets the element's weight.
    • pick

      public T pick(@Nonnull Random rand)
    • size

      public int size()
      Size of the map.
    • elements

      @Nonnull public List<T> elements()
    • isImmutable

      public boolean isImmutable()
      Checks if the map is immutable.
    • forEach

      public void forEach(@Nonnull BiConsumer<T,Double> consumer)
    • toString

      @Nonnull public String toString()
      Overrides:
      toString in class Object