UnsupportedOperationException - 此集合不支持removeAll方法(Java集合)

Nim*_*sky 9 java collections

Set<Badge> availableBadges = myService.getAvailableBadges();
List<Badge> allBadges = Arrays.asList(Badge.values());
allBadges.removeAll(availableBadges);
/* Badge is an enumn */
Run Code Online (Sandbox Code Playgroud)

什么集合支持删除所有?

And*_*niy 23

Arrays.asList返回部分不可修改的实现(部分remove*方法 - 感谢@LouisWasserman的注释)List接口.

编辑1:ArrayList在其上使用包装:new ArrayList<Badge>(allBadges);

  • `Arrays.asList`并不是完全不可修改的; 它支持`set`,但不支持`remove`或`add`. (3认同)