diff --git a/CHANGELOG.md b/CHANGELOG.md index 801b28991..5efe4df3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ ### Bug修复 * 【core】 修复NetUtil.getUsableLocalPort问题(pr#69@Gitee) * 【core】 修复MathUtil.arrangementSelect重复元素导致无结果问题(issue#529@Gitee) +* 【core】 修复RandomUtil.randomEleSet越界问题(issue#535@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java index cc4cbd803..f49888325 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java @@ -14,6 +14,7 @@ import java.util.Random; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; @@ -355,13 +356,13 @@ public class RandomUtil { * @throws IllegalArgumentException 需要的长度大于给定集合非重复总数 */ public static Set randomEleSet(Collection collection, int count) { - ArrayList source = new ArrayList<>(new HashSet<>(collection)); + final ArrayList source = CollUtil.distinct(collection); if (count > source.size()) { throw new IllegalArgumentException("Count is larger than collection distinct size !"); } final HashSet result = new HashSet(count); - int limit = collection.size(); + int limit = source.size(); while (result.size() < count) { result.add(randomEle(source, limit)); }