List.addAll throwing UnsupportedOperationException when trying to add another list [duplicate]
Solution 1:
Arrays.asList
returns a fixed sized list backed by an array, and you can't add elements to it.
You can create a modifiable list to make addAll
work :
List<String> supportedTypes = new ArrayList<String>(Arrays.asList("6500", "7600", "8700"));
Solution 2:
This error also happens when the list is initialized with Collections.emptyList()
, which is immutable:
List<String> myList = Collections.emptyList();
Instead, initialize it with a mutable list. For example
List<String> myList = new ArrayList<>();
Solution 3:
Arrays.asList returns a fixed-size list.
If you to want be able to add elements to the list, do:
List<String> supportedTypes = new ArrayList<>(Arrays.asList("6500", "7600"});
supportedTypes.addAll(Arrays.asList(supportTypes.split(",")));