字符串实习生方法我看着从JavaDays代码,作者说,这种做法有可能是用于存储像模拟字符串转换为字符串实习生方法重复数据删除在ConcurrentHashMap的
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;
}
}
请给我解释一下,什么是非常有效的在这条线的概率的效果:
if (ThreadLocalRandom.current().nextInt() > prob) return t;
这是从Java天原介绍https://shipilev.net/talks/jpoint-April2015-string-catechism.pdf (第56幻灯片)
对我来说,它看起来像'如果(ThreadLocalRandom.current()nextInt()>概率。)'语句设计,使返回的字符串,不存储输入值在地图中,它取决于设定的概率。 – pacman
如果'prob'很大,会发生什么?如果它很小,会发生什么? –
@Oliver Charlesworth我想'prob'是概率百分比 – pacman