MCPcopy Create free account
hub / github.com/boxbeam/RedLib / fromCollection

Method fromCollection

src/redempt/redlib/misc/WeightedRandom.java:60–66  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

fromDoubleMapMethod · 0.95
applyMethod · 0.65

Tested by

no test coverage detected