/**
*
* @param groupConfigMap
* key是要选择的对象
* value是这个对象的权重
* @return
*/
public static Long calculateGroupConfigId(Map<Long, Long> groupConfigMap) {
if (CollectionUtils.isEmpty(groupConfigMap)) {
return null;
}
Long sum = 0L;
for (Map.Entry<Long, Long> entry : groupConfigMap.entrySet()) {
sum += entry.getValue();
}
if (sum == 0L) {
return null;
}
Long random = ThreadLocalRandom.current().nextLong(sum);
Long n = 0L;
for (Map.Entry<Long, Long> entry : groupConfigMap.entrySet()) {
n = n + entry.getValue();
if (random < n) {
return entry.getKey();
}
}
return null;
}