-1

I have two strings having comma separated values, say one having numbers from 1 to 10 and another having prime numbers. I want to

remove prime numbers from numbers

.

Here's my code snippet:

String numbers = "1,2,3,4,5,6,7,8,9,10";
String prime = "2,3,5,7";

List<String> numList = Arrays.asList(numbers.split(","));
numList.removeAll(Arrays.asList(prime.split(",")));

I'm getting UnsupportedOperationException. Any help would be appreciated.

Venkat
  • 2,604
  • 6
  • 26
  • 36

1 Answers1

6

Try to use this:-

List<String> numList = new ArrayList<>(Arrays.asList(numbers.split(",")));

If you look at the docs:-

UnsupportedOperationException - if the removeAll operation is not supported by this list

Arrays.asList returns a fixed-size list, and hence, you get the UnsupportedOperationException when you try to perform the remove operation on that.

Rahul
  • 44,383
  • 11
  • 84
  • 103