Class WeightedList<T>

java.lang.Object
com.marcpg.libpg.storing.WeightedList<T>
Type Parameters:
T - The type of this weighted list's items.
All Implemented Interfaces:
Iterable<T>, Collection<T>

public class WeightedList<T> extends Object implements Collection<T>
A list that can be used to retrieve random items based on weights. Items with a higher weight will be more likely to be chosen.
If you don't care about the weight of a list and just need randomization, you can take a look at Randomizer.fromCollection(Collection) this}.
  • Constructor Details

    • WeightedList

      public WeightedList()
  • Method Details

    • add

      public void add(T item, double weight)
      Adds a new item with the defined weight to the list.
      Parameters:
      item - The item to add to the list.
      weight - The item's weight.
    • random

      public T random()
      Retrieves a random item from this weighted list by using binary search. This will take O(log n) times and therefor is logarithmic.
      Returns:
      A random item from this weighted list, where items with a higher weight are more likely to be chosen.
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<T>
    • iterator

      @NotNull public @NotNull Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
    • toArray

      public Object @NotNull [] toArray()
      Specified by:
      toArray in interface Collection<T>
    • toArray

      public <T1> T1 @NotNull [] toArray(T1 @NotNull [] a)
      Specified by:
      toArray in interface Collection<T>
    • add

      public boolean add(T item)
      Adds the specified item to this weighted list with the default weight of 1.0. If you want control over the weight, see this!
      Specified by:
      add in interface Collection<T>
      Parameters:
      item - The item to add to this list.
      Returns:
      Always true!
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<T>
    • containsAll

      public boolean containsAll(@NotNull @NotNull Collection<?> c)
      Specified by:
      containsAll in interface Collection<T>
    • addAll

      public boolean addAll(@NotNull @NotNull Collection<? extends T> c)
      Adds all items from the collection to this weighted list, with all having the default weight of 1.0.
      Specified by:
      addAll in interface Collection<T>
      Parameters:
      c - Collection containing elements to be added to this weighted list.
      Returns:
      If this weighted list has been modified or not. Always true.
    • removeAll

      public boolean removeAll(@NotNull @NotNull Collection<?> c)
      Removes all items from the collection from this weighted list.
      Specified by:
      removeAll in interface Collection<T>
      Parameters:
      c - Collection containing elements to be removed from this weighted list.
      Returns:
      If this weighted list has been modified or not.
    • retainAll

      public boolean retainAll(@NotNull @NotNull Collection<?> c)
      Specified by:
      retainAll in interface Collection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>