Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException

Tags:

I am preparing for an OCPJP 8 exam for the next 2 months and currently I this one got my attention as I dont understand why

public class BiPredicateTest {     public static void main(String[] args) {         BiPredicate<List<Integer>, Integer> containsInt = List::contains;         List<Integer> ints = java.util.Arrays.asList(1,20,20);         ints.add(1);         ints.add(20);         ints.add(20);         System.out.println(containsInt.test(ints, 20));          BiConsumer<List<Integer>, Integer> listInt = BiPredicateTest::consumeMe;         listInt.accept(ints, 15);      }      public static void consumeMe(List<Integer> ints, int num) {         ints.removeIf(i -> i>num);         ints.forEach(System.out::println);     } } 

this clearly is going to compile OK! but when you run it you will see the exception like this

C:\Users\user\Documents>javac BiPredicateTest.java  C:\Users\user\Documents>java BiPredicateTest true Exception in thread "main" java.lang.UnsupportedOperationException         at java.util.AbstractList.remove(AbstractList.java:161)         at java.util.AbstractList$Itr.remove(AbstractList.java:374)         at java.util.Collection.removeIf(Collection.java:415)         at BiPredicateTest.consumeMe(BiPredicateTest.java:22)         at BiPredicateTest.main(BiPredicateTest.java:17) 

I need some help here to understand why the asList method is not working with removeIf? i assume it will return an instance of ArrayList which implements removeIf method!.

Any answer will be appreciated.

cheers!

like image 242
mark ortiz Avatar asked Mar 25 '17 18:03

mark ortiz


People also ask

How do I fix Java Lang UnsupportedOperationException?

The UnsupportedOperationException can be resolved by using a mutable collection, such as ArrayList , which can be modified. An unmodifiable collection or data structure should not be attempted to be modified.

What does Arrays asList () do?

The asList() method of java. util. Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.

What Arrays return asList?

asList returns a fixed-size list that is​ backed by the specified array; the returned list is serializable and allows random access.

Does Arrays asList return Unmodifiable list?

Arrays. asList() method returns a fixed-size list backed by the specified array. Since an array cannot be structurally modified, it is impossible to add elements to the list or remove elements from it. The list will throw an UnsupportedOperationException if any resize operation is performed on it.


Video Answer


1 Answers

java.util.Arrays.asList() produces a list from which it is impossible to remove elements, so it throws on a removal attempt.

You could wrap it with ArrayList:

List<Integer> ints = new java.util.ArrayList<>(java.util.Arrays.asList(1,20,20)); 

Update

Arrays.asList() returns return new ArrayList<>(a); where ArrayList is not java.util.ArrayList, but java.util.Arrays.ArrayList (internal class), which does not allow removal.

like image 185
Roman Puchkovskiy Avatar answered Oct 15 '22 13:10

Roman Puchkovskiy