fix funny ProbabilityCollection bug

This commit is contained in:
dfsek 2021-06-14 19:45:41 -07:00
parent 68aa38a51a
commit e0d7b03820

View File

@ -49,10 +49,14 @@ public class ProbabilityCollection<E> implements Collection<E> {
public <T> ProbabilityCollection<T> map(Function<E, T> mapper, boolean carryNull) {
ProbabilityCollection<T> newCollection = new ProbabilityCollection<>();
cont.forEach((o, count) -> {
if(o != null) newCollection.add(mapper.apply(o), count.get());
else if(carryNull) newCollection.add(null, count.get());
});
newCollection.array = new Object[array.length];
Map<E, T> cache = new HashMap<>();
for(int i = 0; i < array.length; i++) {
if(carryNull && array[i] == null) continue;
newCollection.array[i] = cache.computeIfAbsent((E) array[i], mapper);
}
return newCollection;
}