2

I would like to remove all list elements, which their last split char not founded as a key in some map. I use following code:

   List<String> myList = Arrays.asList("a.b.c,c.d.e".split(","));
   myList.add("a.b.c");
   Map myMap = new HashMap();
   myMap.put("a","dumy");
   myList.removeIf(el->!myMap.containsKey(el.substring(el.lastIndexOf('.') + 1)));

I get following error: Method threw 'java.lang.UnsupportedOperationException' exception.

any idea what I did wrong?

Holger
  • 285,553
  • 42
  • 434
  • 765
user1167753
  • 705
  • 3
  • 7
  • 15
  • Can you add the code that produces this error? These types have spelling errors etc. – Jeppe Feb 22 '19 at 15:49
  • the last code line cuses the error. i have updated the code – user1167753 Feb 22 '19 at 15:52
  • Your code is working fine. I assuming that you are showing us the wrong code and in actual code, you are using something like `Arrays.asList()` or immutable list. – ByeBye Feb 22 '19 at 15:54
  • @ByeBye Aside `Arrays.asList` is not immutable. But yes, in general, the operation to remove an element from an immutable collection might cause `UnsupportedOperationException` – Naman Feb 22 '19 at 15:56
  • Well I build the list as followes: List myList=Arrays.asList(cats.split(","));. Is this the reason for that error? how to solve it ? – user1167753 Feb 22 '19 at 16:06
  • 1
    @user1167753 Unless you share the complete context, no one would be able to provide a solution to it. Please edit the question and update with a minimal reproducible example for the problem. – Naman Feb 22 '19 at 16:08
  • code updated. first line seems to cuse the issue. why and how to solve it? THANKS – user1167753 Feb 22 '19 at 16:10

3 Answers3

8

Arrays.asList() creates fixed-size list. You cannot add new elements or remove elements.

You can use new ArrayList<>(Arrays.asList(el))

ByeBye
  • 6,650
  • 5
  • 30
  • 63
1

To further explain a bit the reason behind why adding (and deleting) from an ArrayList created through Arrays.asList (array) is not permitted and throws UnsupportedOperationException, first we know that what this Arrays.asList(...) does is to convert an array to its equivalent instance of ArrayList:

String[] strArr = new String[]{"a","b","c"};
List<String> strList = Arrays.asList(strArr);

What's happening here is your strArr is actually a backing array for strList, meaning if you modify strArr[0] then the value at strList.get(0) will also follow suit, and vice versa. Yes, their values will sync and will remain consistent to each other. This is why you cannot do an addition or deletion for strList because the backing array will cry and panic as it cannot fulfill it as it is fixed-length (arrays are always fixed size as we know, if you need to resize an array, you need to create a whole new array altogether, resize an existing one is not possible); thus it throws UnsupportedOperationException.

So the solution, as already mentioned by @ByeBye, is to wrap it with an ArrayList so it would be a new independent ArrayList with no backing array whatsoever and has freedom to do addition and deletion.

lorraine batol
  • 6,001
  • 16
  • 55
  • 114
-1

solved by adjusting code line as followes:

List<String> myList = Lists.newArrayList(Arrays.asList("a.b.c,c.d.e".split(",")));
user1167753
  • 705
  • 3
  • 7
  • 15