Package com.marcpg.libpg.storing
Class WeightedList<T>
java.lang.Object
com.marcpg.libpg.storing.WeightedList<T>
- Type Parameters:
T
- The type of this weighted list's items.
- All Implemented Interfaces:
Iterable<T>
,Collection<T>
A list that can be used to retrieve random items based on weights. Items with a higher weight
will be more likely to be chosen.
If you don't care about the weight of a list and just need randomization, you can take a look at
If you don't care about the weight of a list and just need randomization, you can take a look at
Randomizer.fromCollection(Collection)
this}.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds the specified item to this weighted list with the default weight of 1.0.void
Adds a new item with the defined weight to the list.boolean
addAll
(@NotNull Collection<? extends T> c) Adds all items from the collection to this weighted list, with all having the default weight of 1.0.void
clear()
boolean
boolean
containsAll
(@NotNull Collection<?> c) boolean
isEmpty()
iterator()
random()
Retrieves a random item from this weighted list by using binary search.boolean
boolean
removeAll
(@NotNull Collection<?> c) Removes all items from the collection from this weighted list.boolean
retainAll
(@NotNull Collection<?> c) int
size()
Object @NotNull []
toArray()
<T1> T1 @NotNull []
toArray
(T1 @NotNull [] a) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
WeightedList
public WeightedList()
-
-
Method Details
-
add
Adds a new item with the defined weight to the list.- Parameters:
item
- The item to add to the list.weight
- The item's weight.
-
random
Retrieves a random item from this weighted list by using binary search. This will take O(log n) times and therefor is logarithmic.- Returns:
- A random item from this weighted list, where items with a higher weight are more likely to be chosen.
-
size
public int size()- Specified by:
size
in interfaceCollection<T>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<T>
-
contains
- Specified by:
contains
in interfaceCollection<T>
-
iterator
-
toArray
- Specified by:
toArray
in interfaceCollection<T>
-
toArray
public <T1> T1 @NotNull [] toArray(T1 @NotNull [] a) - Specified by:
toArray
in interfaceCollection<T>
-
add
Adds the specified item to this weighted list with the default weight of 1.0. If you want control over the weight, seethis
!- Specified by:
add
in interfaceCollection<T>
- Parameters:
item
- The item to add to this list.- Returns:
- Always true!
-
remove
- Specified by:
remove
in interfaceCollection<T>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<T>
-
addAll
Adds all items from the collection to this weighted list, with all having the default weight of 1.0.- Specified by:
addAll
in interfaceCollection<T>
- Parameters:
c
- Collection containing elements to be added to this weighted list.- Returns:
- If this weighted list has been modified or not. Always true.
-
removeAll
Removes all items from the collection from this weighted list.- Specified by:
removeAll
in interfaceCollection<T>
- Parameters:
c
- Collection containing elements to be removed from this weighted list.- Returns:
- If this weighted list has been modified or not.
-
retainAll
- Specified by:
retainAll
in interfaceCollection<T>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<T>
-