生成随机数列表
1、Java8以前
(1)Math.random
private List<UserEntity> random1() {
ArrayList<UserEntity> list = Lists.newArrayList();
for (int i = 1; i <= 10; i++)
list.add(new UserEntity().setUserName("name" + (int) (Math.random() * 100) + 1));
return list;
}
(2)Random类
private List<UserEntity> random2() {
Random random = new Random();
ArrayList<UserEntity> list = Lists.newArrayList();
for (int i = 1; i <= 10; i++)
list.add(new UserEntity().setUserName("name" + random.nextInt(100)));
return list;
}
2、Java8以后(Stream)
private List<UserEntity> randomUserEntities() {
return new Random()
.ints(0, 100)
.limit(10)
.mapToObj(val -> new UserEntity().setUserName("name" + val))
.collect(Collectors.toList());
}