Package io.fi0x.javadatastructures
Class WeightedList<Type>
java.lang.Object
io.fi0x.javadatastructures.WeightedList<Type>
- Type Parameters:
Type- The type ofObjectthat should be stored in thisWeightedList.
This class represents a weighted list that can be used for easy weighted randomization.
-
Constructor Summary
ConstructorsConstructorDescriptionWeightedList(Random random) The main constructor of this class requires aRandomto be able to return random elements later. -
Method Summary
Modifier and TypeMethodDescriptionvoidThis method allows new elements to be added to theWeightedList.get(double weightIndex) This method returns anObjectfrom theWeightedListwith a specified weighted value.random()This method returns a random element from theWeightedList.This method returns a random element from theWeightedListand removes it.intsize()This method returns the amount of entries that are stored inside theWeightedList.
-
Constructor Details
-
Method Details
-
add
This method allows new elements to be added to theWeightedList.- Parameters:
weight- The weight which the element has in theWeightedList.item- The item that should be added.
-
get
This method returns anObjectfrom theWeightedListwith a specified weighted value. The weightIndex does not represent a specific weight of an entry. The element at the position of the parameter is dependent on the order in which elements got added and what the weights of the previous elements are.- Parameters:
weightIndex- The number, at which theWeightedListshould be searched.- Returns:
- The element that is stored at the weighted location.
-
random
This method returns a random element from theWeightedList. Elements with higher weights have a higher chance to get selected.- Returns:
- A random element from the
WeightedList.
-
randomRemove
This method returns a random element from theWeightedListand removes it. Elements with higher weights have a higher change to get selected.- Returns:
- A random element from the
WeightedList, that is also removed by this method.
-
size
public int size()This method returns the amount of entries that are stored inside theWeightedList.- Returns:
- The size of the
WeightedList.
-