java.util.Arrays.asList与removeIf一起使用时抛出UnsupportedOperationException [英] java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException
问题描述
我正准备接下来2个月的OCPJP 8考试
,目前我已经引起了我的注意,因为我不明白为什么
public class BiPredicateTest {
public static void main(String [] args){
BiPredicate< List< Integer>,Integer> containsInt = List :: contains;
列表<整数> 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);
}
}
这显然是编译好的!但是当你运行它时你会看到这样的例外
C:\ Users \ user \ Document> javac BiPredicateTest .java
C:\ Users \ user \ Document> java BiPredicateTest
true
线程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)BiPredicateTest.consumeMe(BiPredicateTest.java:22)
BiPredicateTest.main $($)$
$
>
我需要一些帮助来理解为什么asList方法不能与removeIf一起使用?我假设它将返回一个实现removeIf方法!的ArrayList实例。
任何答案都将不胜感激。
欢呼!
解决方案 java.util.Arrays.asList()
生成一个列表从中不可能删除元素,因此它会引发删除尝试。
你可以用 ArrayList
:
List< Integer> ints = new java.util.ArrayList<>(java.util.Arrays.asList(1,20,20));
更新
Arrays.asList()
返回返回新的ArrayList<>(a);
其中 ArrayList
不是 java.util.ArrayList
,但 java.util.Arrays.ArrayList
(内部类),其中不允许删除。
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!
解决方案 java.util.Arrays.asList()
produces an 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.
这篇关于java.util.Arrays.asList与removeIf一起使用时抛出UnsupportedOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!