随机获取部分List集合

随机返回list对象

  /**
     * 返回随机List
     * @param list 备选
     * @param selected 备选数量
     * @return
     */
    public  List getRandomNum(List list, int selected) {
        List<Object> reList = new ArrayList<Object>();

        Random random = new Random();
        // 先抽取,备选数量的个数
        if (list.size() >= selected) {
            for (int i = 0; i < selected; i++) {
                // 随机数的范围为0-list.size()-1;
                int target = random.nextInt(list.size());
                reList.add(list.get(target));
                list.remove(target);
            }
        } else {
            selected = list.size();
            for (int i = 0; i < selected; i++) {
                // 随机数的范围为0-list.size()-1;
                int target = random.nextInt(list.size());
                reList.add(list.get(target));
                list.remove(target);
            }
        }
      
        return reList;
    }

 

相关文章:

  • 2022-12-23
  • 2021-12-30
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-04
  • 2022-01-07
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案
粤ICP备22038628号Powered By WordPress