使用Collections.sort()时,报java.lang.UnsupportedOperationException

from stackoverflow

https://stackoverflow.com/questions/21854353/why-does-collections-sort-throw-unsupported-operation-exception-while-sorting-by

    private List<MaterialVo> sortByLimit(List<MaterialVo> voList ){
        if(CollectionUtils.isEmpty(voList)){
           return null;
        }
//不明白为什么 这时我的voList变成了UNmodified状态,想要sort还要重新构造一下(如下)
        voList = new ArrayList<>(voList);//由于java.lang.UnsupportedOperationException,所以把list重新构造了一遍
        voList.sort((MaterialVo o1, MaterialVo o2) -> {
            if(null != o1 && null != o2){
                String flag1 = o1.getFlag();
                String flag2 = o2.getFlag();
                if(!StringUtils.isEmpty(flag1) && !StringUtils.isEmpty(flag2)){
                    int i = Integer.parseInt(flag2) - Integer.parseInt(flag1);
                    if(i == 0){//flag相同的时候 按照更新时间排一下顺序
                        LocalDateTime updateTime1 = o1.getUpdateTime();
                        LocalDateTime updateTime2 = o2.getUpdateTime();
                        if(null != updateTime1 && null != updateTime2){
                            boolean after = updateTime1.isAfter(updateTime2);
                            //如果前者晚于后者创建,则把前者排在前边
                            if(after){
                                return -1;
                            }else{
                                return 1;
                            }
                        }
                    }else {
                        return i;
                    }
                }
            }
            return 0;
        });

        return voList;
    }

 


版权声明:本文为u014662858原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。