[Arrays.asList作List]+[之后Iterator]的坑Caused by: java.lang.UnsupportedOperationException

搞了个定时任务,报错,差点以为是定时任务不支持事务呢,生产日志打的缺,不如IDEA打的多,坑啊。不是的。是其他异常了
Caused by: java.lang.UnsupportedOperationException
看发生位置,是在Iterator 里
while (it.hasNext()) {
String str = (String) it.next();
if (str.endsWith(“0000”)) {
// this
it.remove();
}
}

   @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void completePeoplesInOrIds() 
        List<OaSceneEntity> sceneList=oaSceneService.selectList(new EntityWrapper<OaSceneEntity>().eq("scene_type",2));
        if(!CollectionUtils.isEmpty(sceneList)){
            for (OaSceneEntity oaSceneEntity : sceneList) {
                //查询设备的部门人员和已配的人员增减,进行相应增删
                addOrSubPeoplesByOrIds(oaSceneEntity);
            }
        }

    }

   private void addOrSubPeoplesByOrIds(OaSceneEntity oaSceneEntity ){
        String orIds=oaSceneEntity.getOrIds();
        if(StringUtils.isNotBlank(orIds)){
            //异常的原因分析:Arrays.asList()返回由[原始数组]支持的列表并且[您对列表所做的更改也会反映在您传入的数组中],坑啊,不是具体的List/平时玩耍理解上的真正的List,而后续的iterator会抛UnsupportedOperationException(数组不支持remove)
            List<String> orIdList=Arrays.asList(orIds.split(","));
            List<String> hrIdList = null;
            if (!CollectionUtils.isEmpty(orIdList)) {
                hrIdList = getHrIdsFromOrIds(orIdList);
            }

处理:for each循环删asList而来的List的元素时会异常。采用Iterator

  public List<String> getHrIdsFromOrIds(List<String> orIdListOrigin) {
        //大部门id后面有0000标志。只可能是选全选时出现了这个
        //this
        List<String> orIdList = new ArrayList<>(orIdListOrigin);
        Iterator<String> it = orIdList.iterator();
        while (it.hasNext()) {
            String str = (String) it.next();
            if (str.endsWith("0000")) {
                it.remove();
            }
        }

参考博文
使用iterator.remove()时UnsupportedOperationException(UnsupportedOperationException when using iterator.remove())

iterator.remove() java.lang.UnsupportedOperationException异常原因及解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值