When I try to remove the element from the list using removeIf()
, It is throwing UnsupportedOperationException
public class T {
public static void main(String[] args) {
String[] arr = new String[] { "1", "2", "3" };
List<String> stringList = Arrays.asList(arr);
stringList.removeIf((String string) -> string.equals("2"));
}
}
Can some one help me understand why this is happening and how can I rectify this ?
Arrays.asList(arr)
returns a fixed sized List
, so you can't add or remove elements from it (only replace existing elements).
Create an ArrayList
instead:
List<String> stringList = new ArrayList<>(Arrays.asList(arr));
Since you are explicitly calling Arrays.asList
, consider the alternative of not doing that, and create the filtered list directly:
Stream.of(arr)
.filter(string -> !string.equals("2"))
.collect(Collectors.toList())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With