Package net.apartium.cocoabeans.collect
Class WeightSet<E>
java.lang.Object
net.apartium.cocoabeans.collect.WeightSet<E>
- Type Parameters:
E- element type
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>
A set implementation with weighted keys
This class is not thread safe.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructors an empty weight set instanceConstruct a weight set instance from given mapA copy constructor to create a new instance of WeightSet with same elements as given weight set -
Method Summary
Modifier and TypeMethodDescriptionbooleanYou should preferput(Object, double)over this method, since you can specify weight with itbooleanaddAll(@NotNull Collection<? extends E> collection) voidclear()booleanbooleancontainsAll(@NotNull Collection<?> collection) getPercentage(E element) Get weight of given element as percentage of total weight in weight setGet weight associated with given elementdoublegetWeightOrDefault(E element, double defaultValue) Get weight associated with given elementbooleanisEmpty()iterator()pickMany(int num) Randomly pick given number of elements from set based on their weight.Randomly pick given number of elements from set based on their weight.pickOne()Randomly pick one element from set based on their weight.Randomly pick one element from set based on their weight.Insert an element with given weight into the setvoidPut all entries from given map into current instance.voidPut all entries from given weight set instance into the current instancebooleanbooleanremoveAll(@NotNull Collection<?> collection) booleanretainAll(@NotNull Collection<?> collection) intsize()@NotNull Object @NotNull []toArray()<T> @NotNull T @NotNull []toArray(T @NotNull [] ts) doubleGet total weight of all elements in setvalues()Get all elements in this weight setMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
equals, hashCode, spliterator
-
Constructor Details
-
WeightSet
public WeightSet()Constructors an empty weight set instance -
WeightSet
A copy constructor to create a new instance of WeightSet with same elements as given weight set- Parameters:
weightSet- given weight set
-
WeightSet
Construct a weight set instance from given map- Parameters:
map- weight set base map, consisting of elements as keys and weight as values
-
-
Method Details
-
put
Insert an element with given weight into the set- Parameters:
element- element to insertweight- weight of given element- Returns:
- optional consisting of weight previously associated with given element, empty if element was not in the set
-
putAll
Put all entries from given map into current instance. You should preferputAll(WeightSet)instead of this method.- Parameters:
map- weight set base map, consisting of elements as keys and weight as values- See Also:
-
putAll
Put all entries from given weight set instance into the current instance- Parameters:
weightSet- weight set to merge into the current one- See Also:
-
clear
public void clear() -
getWeight
Get weight associated with given element- Parameters:
element- element- Returns:
- weight of given element, or empty if not in set
- See Also:
-
getWeightOrDefault
Get weight associated with given element- Parameters:
element- elementdefaultValue- default weight to return if element is not in set- Returns:
- weight of given element, or given default value if element not in set
- See Also:
-
getPercentage
Get weight of given element as percentage of total weight in weight set- Parameters:
element- element- Returns:
- weight of given element as %, or empty if element is not in set
-
values
Get all elements in this weight set- Returns:
- unmodifiable set consisting of all elements of this set
-
totalWeight
public double totalWeight()Get total weight of all elements in set- Returns:
- total weight of all elements in set
-
remove
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
iterator
-
toArray
-
toArray
@NotNull public <T> @NotNull T @NotNull [] toArray(T @NotNull [] ts) -
add
You should preferput(Object, double)over this method, since you can specify weight with it -
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>
-
addAll
-
retainAll
-
removeAll
-
pickOne
Randomly pick one element from set based on their weight. The higher the weight the bigger chance it has of being the result.- Returns:
- picked element, or null if list is empty
- See Also:
-
pickOne
Randomly pick one element from set based on their weight. The higher the weight the bigger chance it has of being the result.- Parameters:
random- random instance to provide random number- Returns:
- picked element, or null if list is empty
- See Also:
-
pickMany
Randomly pick given number of elements from set based on their weight. The higher the weight the bigger chance it has of being in the resulting weightset.- Parameters:
num- number of elements to return- Returns:
- a subset of this weight set with random picks
- See Also:
-
pickMany
Randomly pick given number of elements from set based on their weight. The higher the weight the bigger chance it has of being in the resulting weightset.- Parameters:
num- number of elements to returnrandom- random instance to provide random number- Returns:
- a subset of this weight set with random picks
- See Also:
-