Java 随机抽取List集合的数据

 /**
 * 查询前天所有的报废数据
 * @return
 */
public Map<String,Object> findAllAudit(){
    LOGGER.info("执行该方法的时间"+new SimpleDateFormat("yy-MM--dd HH-mm-ss").format(new Date()));
    Map<String,Object> map = new HashMap<String, Object>();
    Map<String,String> hashMap = new HashMap<String,String>();
    hashMap.put("recordStatusNum","3");
    hashMap.put("deleteMark","-1");
    try {
        List<IllegelAudit>  auditList = this.iIllegelAuditService.getAll("selectScrapByStatusAndDeleMark",hashMap);
        Integer allCount = (int) Math.ceil(auditList.size()*0.01);
        System.out.println("查询的数量"+allCount);*/
        map.put("auditList",auditList);
        map.put("allCount",allCount);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}

/**
 * 每天凌晨的0点10分更新数据
 */
public void updateFlag(){
    LOGGER.info("执行该方法的时间"+new SimpleDateFormat("yy-MM--dd HH-mm-ss").format(new Date()));
    Map<String,Object> map = findAllAudit();
    //取得recordCode的集合
    try{
        List list = getRandomList((List) map.get("auditList"),(Integer) map.get("allCount"));
        Map hashMap = new HashMap();
        hashMap.put("codelist",list);
        if(list!=null){
            this.iIllegelAuditService.update("updateScrapStstus",hashMap);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
/**
 * @function:从list中随机抽取若干不重复元素
 * @param paramList:被抽取list
 * @param count:抽取元素的个数
 * @return:由抽取元素组成的新list
 */
public  List getRandomList(List<IllegelAudit> paramList,int count){
    if(count==0){
        return null;
    }
    Random random=new Random();
    List<Integer> tempList=new ArrayList<Integer>();//临时存放产生的list索引,去除重复的索引
    List newList=new ArrayList();//生成新的list集合
    int temp=0;
    if(count<=1){//如果数据小于1,取一条数据
        temp = random.nextInt(paramList.size());
        newList.add(paramList.get(temp).getRecordCode());
    }else {
        for(int i=0;i<Math.ceil(count);i++){
            temp=random.nextInt(paramList.size());//初始化一个随机数,将产生的随机数作为被抽list的索引
            if(!tempList.contains(temp)){//判断随机抽取的随机数
                tempList.add(temp);
                newList.add(paramList.get(temp).getRecordCode());
            }
            else{
                i--;
            }
        }
    }
    return newList;
}

}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值