Class WeightedSet

  • All Implemented Interfaces:
    kotlin.collections.Collection , kotlin.collections.Iterable , kotlin.collections.MutableCollection , kotlin.collections.MutableIterable , kotlin.collections.MutableSet , kotlin.collections.Set

    
    public final class WeightedSet<E extends Object>
    extends AbstractMutableSet<WeightedSet.Element<E>>
                        

    A set containing elements with associated weights. You can retrieve either a specified subset or a single element randomly, with the probability of each element being chosen proportional to its weight.

    For example, if you have elements A, B, and C with weights 0.25, 0.25, and 0.5 respectively, element C will be chosen approximately 50% of the time, while A and B will each be chosen about 25% of the time.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public final class WeightedSet.Element
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      Integer getSize()
      final Set<E> getElements()
      final Set<E> getRandomSubset(Integer size, Random random) Returns a random subset of the specified size from the set, with selection probability based on weights.
      final Set<E> getRandomSubset(Integer size) Returns a random subset of the specified size from the set, with selection probability based on weights.
      final E getRandom(Random random) Returns a single random element from the set, with selection probability based on weights.
      final E getRandom() Returns a single random element from the set, with selection probability based on weights.
      Boolean add(WeightedSet.Element<E> element)
      Iterator<WeightedSet.Element<E>> iterator()
      • Methods inherited from class kotlin.collections.MutableSet

        addAll, clear, remove, removeAll, retainAll
      • Methods inherited from class kotlin.collections.Set

        contains, containsAll, isEmpty, spliterator
      • Methods inherited from class kotlin.collections.Iterable

        forEach
      • Methods inherited from class kotlin.collections.Collection

        parallelStream, stream, toArray
      • Methods inherited from class kotlin.collections.MutableCollection

        removeIf
      • Methods inherited from class java.util.AbstractCollection

        toArray, toArray
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WeightedSet

        WeightedSet(E value, Float weight)
      • WeightedSet

        WeightedSet()
    • Method Detail

      • getRandomSubset

        @JvmOverloads() final Set<E> getRandomSubset(Integer size, Random random)

        Returns a random subset of the specified size from the set, with selection probability based on weights. The subset will contain unique elements and always be of the requested size.

        Parameters:
        size - The number of unique elements to select.
        random - An optional Random instance to use for selection.
      • getRandomSubset

        @JvmOverloads() final Set<E> getRandomSubset(Integer size)

        Returns a random subset of the specified size from the set, with selection probability based on weights. The subset will contain unique elements and always be of the requested size.

        Parameters:
        size - The number of unique elements to select.
      • getRandom

        @JvmOverloads() final E getRandom(Random random)

        Returns a single random element from the set, with selection probability based on weights.

        Parameters:
        random - An optional Random instance to use for selection.
      • getRandom

        @JvmOverloads() final E getRandom()

        Returns a single random element from the set, with selection probability based on weights.