2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Why I get UnsupportedOperationException when trying to remove from the List?

I have a method to which a string like "0000-b58c-4008-8575-9999" will be send as a parameter .That method has to truncate first and last set of characters(0000 and 9999 in this case)  and must only send the middle sets(b58c-4008-8575). so i have written below code

public String commajoinedIdentifier(String identifier){

String[] split = identifier.split("-");   

List<String> list=Arrays.asList(split);



return StringUtils.join(list, ", ");


but its throwing   UnsupportedOperationException.


Here there is one important point to note : Arrays.asList returning a fixed-size list
From the API:

Arrays.asList: Returns a fixed-size list backed by the specified array.

You can't add to it; you can't remove from it. You can't structurally modify the List.

Create a LinkedList, which supports faster remove.

List<String> list = new LinkedList<String>(Arrays.asList(split));

Answer is