Class WeightedList<Type>

java.lang.Object
io.fi0x.javadatastructures.WeightedList<Type>
Type Parameters:
Type - The type of Object that should be stored in this WeightedList.

public class WeightedList<Type> extends Object
This class represents a weighted list that can be used for easy weighted randomization.
  • Constructor Details

    • WeightedList

      public WeightedList(Random random)
      The main constructor of this class requires a Random to be able to return random elements later.
      Parameters:
      random - The Random that should be used. If null, a new Random will be generated.
  • Method Details

    • add

      public void add(float weight, Type item)
      This method allows new elements to be added to the WeightedList.
      Parameters:
      weight - The weight which the element has in the WeightedList.
      item - The item that should be added.
    • get

      public Type get(double weightIndex)
      This method returns an Object from the WeightedList with a specified weighted value. The weightIndex does not represent a specific weight of an entry. The element at the position of the parameter is dependent on the order in which elements got added and what the weights of the previous elements are.
      Parameters:
      weightIndex - The number, at which the WeightedList should be searched.
      Returns:
      The element that is stored at the weighted location.
    • random

      public Type random()
      This method returns a random element from the WeightedList. Elements with higher weights have a higher chance to get selected.
      Returns:
      A random element from the WeightedList.
    • randomRemove

      public Type randomRemove()
      This method returns a random element from the WeightedList and removes it. Elements with higher weights have a higher change to get selected.
      Returns:
      A random element from the WeightedList, that is also removed by this method.
    • size

      public int size()
      This method returns the amount of entries that are stored inside the WeightedList.
      Returns:
      The size of the WeightedList.