mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add test
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
/**
|
||||
* 提供通用树生成,特点:
|
||||
* <pre>
|
||||
* 1、每个字段可自定义
|
||||
* 2、支持排序 树深度配置,自定义转换器等
|
||||
* 3、支持额外属性扩展
|
||||
* 4、贴心 许多属性,特性都有默认值处理
|
||||
* 5、使用简单 可一行代码生成树
|
||||
* 6、代码简洁轻量无额外依赖
|
||||
* <pre/>
|
||||
* 提供通用树生成,特点:<p>
|
||||
* 1、每个字段可自定义<br>
|
||||
* 2、支持排序 树深度配置,自定义转换器等<br>
|
||||
* 3、支持额外属性扩展<br>
|
||||
* 4、贴心 许多属性,特性都有默认值处理<br>
|
||||
* 5、使用简单 可一行代码生成树<br>
|
||||
* 6、代码简洁轻量无额外依赖
|
||||
* </p>
|
||||
*
|
||||
* @author liangbaikai(https://gitee.com/liangbaikai00/)
|
||||
* @since 5.2.1
|
||||
|
@@ -351,6 +351,28 @@ public class RandomUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机获得列表中的一定量的元素,返回List<br>
|
||||
* 此方法与{@link #randomEles(List, int)} 不同点在于,不会获取重复位置的元素
|
||||
*
|
||||
* @param source 列表
|
||||
* @param count 随机取出的个数
|
||||
* @param <T> 元素类型
|
||||
* @return 随机列表
|
||||
* @since 5.2.1
|
||||
*/
|
||||
public static <T> List<T> randomEleList(List<T> source, int count) {
|
||||
if (count >= source.size()) {
|
||||
return source;
|
||||
}
|
||||
final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count);
|
||||
List<T> result = new ArrayList<>();
|
||||
for (int e : randomList) {
|
||||
result.add(source.get(e));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机获得列表中的一定量的不重复元素,返回Set
|
||||
*
|
||||
@@ -374,6 +396,7 @@ public class RandomUtil {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建指定长度的随机索引
|
||||
*
|
||||
@@ -381,11 +404,11 @@ public class RandomUtil {
|
||||
* @return 随机索引
|
||||
* @since 5.2.1
|
||||
*/
|
||||
public static int[] randomInts(int length){
|
||||
public static int[] randomInts(int length) {
|
||||
final int[] range = ArrayUtil.range(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
int random = randomInt(i,length);
|
||||
ArrayUtil.swap(range,i,random);
|
||||
int random = randomInt(i, length);
|
||||
ArrayUtil.swap(range, i, random);
|
||||
}
|
||||
return range;
|
||||
}
|
||||
|
Reference in New Issue
Block a user