Creates a WeightedRandom from a collection @param collection The collection to convert @param converter The function to convert from the type in the collection to the type in the WeightedRandom @param weightGetter The function to get the weight of an element in the collection @param
(Collection<K> collection, Function<K, T> converter, ToDoubleFunction<K> weightGetter)
| 58 | * @return The populated WeightedRandom |
| 59 | */ |
| 60 | public static <T, K> WeightedRandom<T> fromCollection(Collection<K> collection, Function<K, T> converter, ToDoubleFunction<K> weightGetter) { |
| 61 | Map<T, Double> map = new HashMap<>(); |
| 62 | for (K element : collection) { |
| 63 | map.put(converter.apply(element), weightGetter.applyAsDouble(element)); |
| 64 | } |
| 65 | return fromDoubleMap(map); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a WeightedRandom using the map of weights |
nothing calls this directly
no test coverage detected