Welcome to my website, have a nice day!
Dream it, Do it, Make it!

Java中随机获取List的集合的元素

实现代码如下:

List<VCard> vcards = new ArrayList<>();
// add some elements to vcards
...


// 随机 100
Random r = new Random();
List<VCard> randomList = new ArrayList<>();
while (randomList.size() < 100){
    randomList.add(vcards.get(r.nextInt(vcards.size())));
}

References

  1. How to get a random element from a list with stream api?
赞(0)
未经允许禁止转载:Ddmit » Java中随机获取List的集合的元素

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址