pac*_*man 6 java string dictionary duplicates concurrenthashmap
我看了一篇来自JavaDays的代码,作者说这种方法有概率非常有效,可以将类似String的字符串存储到String实习方法
public class CHMDeduplicator<T> {
private final int prob;
private final Map<T, T> map;
public CHMDeduplicator(double prob) {
this.prob = (int) (Integer.MIN_VALUE + prob * (1L << 32));
this.map = new ConcurrentHashMap<>();
}
public T dedup(T t) {
if (ThreadLocalRandom.current().nextInt() > prob) {
return t;
}
T exist = map.putIfAbsent(t, t);
return (exist == null) ? t : exist;
}
}
Run Code Online (Sandbox Code Playgroud)
请解释一下,这一行中概率的影响是什么:
if (ThreadLocalRandom.current().nextInt() > prob) return t;
Run Code Online (Sandbox Code Playgroud)
这是Java Days的原始演示文稿https://shipilev.net/talks/jpoint-April2015-string-catechism.pdf(56th slide)