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>
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 Summary
ConstructorsConstructorDescriptionThis will initialize a newRandomizedSetwith the internal Set being aLinkedHashSetRandomizedSet(Collection<T> collection) This will initialize a newRandomizedSetwith the internal Set being aLinkedHashSetIt will be populated with elements from the givenCollection, each element will be given a weight of 1.RandomizedSet(Supplier<Set<WeightedNode<T>>> constructor) This will initialize a newRandomizedSetusing the given implementation ofSetRandomizedSet>String< map = new RandomizedSet><(HashSet::new); -
Method Summary
Modifier and TypeMethodDescriptionbooleanThis method adds a new element to this Set with the given weight.voidclear()This method clears this Set and removes all elements from it.booleanThis method returns whether the given element is contained in this SetThis method gives you a randomly selected item from this Set.This method gives you a randomly selected item from this Set.getRandomSubset(int size) This will create a random subset of unique elements from this Set.getRandomSubset(Random random, int size) This will create a random subset of unique elements from this Set.booleanisEmpty()This method returns whether this Set is empty.iterator()This method will provide an infiniteStreamof elements, randomly drawn from thisRandomizedSet.booleanThis method will remove the given Item from this Set.voidThis method updates an element with the given weight.intsize()This method returns the cardinality of this set.stream()This method allows you to stream all elements in this Set.floatThis method returns the sum of all the weights in this set.T[]toArray(IntFunction<T[]> constructor) Returns an Array for all elements contained in this Set.toMap()This method returns a Map that holds all elements from this Set and their associated weights.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
RandomizedSet
public RandomizedSet()This will initialize a newRandomizedSetwith the internal Set being aLinkedHashSet -
RandomizedSet
This will initialize a newRandomizedSetusing the given implementation ofSetRandomizedSet>String< map = new RandomizedSet><(HashSet::new);- Parameters:
constructor- The Constructor for an implementation ofSet
-
RandomizedSet
This will initialize a newRandomizedSetwith the internal Set being aLinkedHashSetIt will be populated with elements from the givenCollection, each element will be given a weight of 1.- Parameters:
collection- ACollectionto 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
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
-
toArray
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
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 addweight- The associated weight- Returns:
- Whether the element was added successfully
-
setWeight
This method updates an element with the given weight. The element must be contained in this Set before calling this method, otherwise anIllegalStateExceptionwill be thrown.- Parameters:
obj- The element in this Setweight- The new weight for this element
-
remove
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
This method allows you to stream all elements in this Set.- Specified by:
streamin interfaceStreamable<T>- Returns:
- A Stream of elements from this Set
-
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
This method gives you a randomly selected item from this Set. The selection is based on their weights. You can specify an instance ofRandom.- Parameters:
random- An instance ofRandomto be used.- Returns:
- A random element from this Set
-
getRandomSubset
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, anIllegalArgumentExceptionwill be thrown.- Parameters:
size- The amount of items to draw from this Set.- Returns:
- A random Subset from this Set.
-
getRandomSubset
This will create a random subset of unique elements from this Set. The selection is based on their weights. You can specify an instance ofRandom. If the size you specify is bigger than the size of this Set, anIllegalArgumentExceptionwill be thrown.- Parameters:
random- An instance ofRandomto be used.size- The amount of items to draw from this Set.- Returns:
- A random Subset from this Set.
-
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
This method will provide an infiniteStreamof elements, randomly drawn from thisRandomizedSet.- Returns:
- An infinite unordered Stream of random elements from this Set
-
toString
-