Class RandomizedSet<T>

java.lang.Object
io.github.bakedlibs.dough.collections.RandomizedSet<T>
Type Parameters:
T - The Type of Element that is stored in this Set
All Implemented Interfaces:
Streamable<T>, Iterable<T>

public class RandomizedSet<T> extends Object implements Iterable<T>, Streamable<T>
This Class represents a collection of elements. Each element is given a weight making the randomized selection of elements weight-based. This collection acts like a Set, it will not allow duplicates. Use getRandom() to draw a random element from this Set. You can also create Subsets of this collection via getRandomSubset(int)
Author:
TheBusyBiscuit
  • Constructor Details

    • RandomizedSet

      public RandomizedSet()
      This will initialize a new RandomizedSet with the internal Set being a LinkedHashSet
    • RandomizedSet

      public RandomizedSet(@Nonnull Supplier<Set<WeightedNode<T>>> constructor)
      This will initialize a new RandomizedSet using the given implementation of Set RandomizedSet>String< map = new RandomizedSet><(HashSet::new);
      Parameters:
      constructor - The Constructor for an implementation of Set
    • RandomizedSet

      public RandomizedSet(@Nonnull Collection<T> collection)
      This will initialize a new RandomizedSet with the internal Set being a LinkedHashSet It will be populated with elements from the given Collection, each element will be given a weight of 1.
      Parameters:
      collection - A Collection to pick elements from, each with the weight of 1.
  • Method Details

    • size

      public int size()
      This method returns the cardinality of this set. The cardinality describes the amount of elements included in that Set.
      Returns:
      The number of elements in this Set
    • sumWeights

      public float sumWeights()
      This method returns the sum of all the weights in this set.
      Returns:
      The sum of all the individual weights from the elements included in this Set.
    • isEmpty

      public boolean isEmpty()
      This method returns whether this Set is empty.
      Returns:
      Whether this Set is empty
    • contains

      public boolean contains(@Nonnull T obj)
      This method returns whether the given element is contained in this Set
      Parameters:
      obj - The element to check for
      Returns:
      Whether the given element is contained in this Set
    • iterator

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

      public T[] toArray(@Nonnull IntFunction<T[]> constructor)
      Returns an Array for all elements contained in this Set.
      Parameters:
      constructor - A reference to an Array constructor
      Returns:
      An Array containing all elements in this Set
    • add

      public boolean add(@Nonnull T obj, float weight)
      This method adds a new element to this Set with the given weight. The weight must be greater than 0.
      Parameters:
      obj - The element to add
      weight - The associated weight
      Returns:
      Whether the element was added successfully
    • setWeight

      public void setWeight(@Nonnull T obj, float weight)
      This method updates an element with the given weight. The element must be contained in this Set before calling this method, otherwise an IllegalStateException will be thrown.
      Parameters:
      obj - The element in this Set
      weight - The new weight for this element
    • remove

      public boolean remove(@Nonnull T obj)
      This method will remove the given Item from this Set. If the element is not contained in the Set, it will return false.
      Parameters:
      obj - The element to remove
      Returns:
      Whether the element was removed successfully
    • clear

      public void clear()
      This method clears this Set and removes all elements from it.
    • stream

      public Stream<T> stream()
      This method allows you to stream all elements in this Set.
      Specified by:
      stream in interface Streamable<T>
      Returns:
      A Stream of elements from this Set
    • getRandom

      public T getRandom()
      This method gives you a randomly selected item from this Set. The selection is based on their weights.
      Returns:
      A random element from this Set
    • getRandom

      public T getRandom(@Nonnull Random random)
      This method gives you a randomly selected item from this Set. The selection is based on their weights. You can specify an instance of Random.
      Parameters:
      random - An instance of Random to be used.
      Returns:
      A random element from this Set
    • getRandomSubset

      public Set<T> getRandomSubset(int size)
      This will create a random subset of unique elements from this Set. The selection is based on their weights. If the size you specify is bigger than the size of this Set, an IllegalArgumentException will be thrown.
      Parameters:
      size - The amount of items to draw from this Set.
      Returns:
      A random Subset from this Set.
    • getRandomSubset

      public Set<T> getRandomSubset(@Nonnull Random random, int size)
      This will create a random subset of unique elements from this Set. The selection is based on their weights. You can specify an instance of Random. If the size you specify is bigger than the size of this Set, an IllegalArgumentException will be thrown.
      Parameters:
      random - An instance of Random to be used.
      size - The amount of items to draw from this Set.
      Returns:
      A random Subset from this Set.
    • toMap

      public Map<T,Float> toMap()
      This method returns a Map that holds all elements from this Set and their associated weights.
      Returns:
      A Map representing this Set's elements and their weights.
    • randomInfiniteStream

      public Stream<T> randomInfiniteStream()
      This method will provide an infinite Stream of elements, randomly drawn from this RandomizedSet.
      Returns:
      An infinite unordered Stream of random elements from this Set
    • toString

      public String toString()
      Overrides:
      toString in class Object