[英]Encountered in Java 8 - JDK-8023339 : j.u.Arrays.asList().removeIf() is “lazy” in throwing java.lang.UnsupportedOperationException
First time with Java 8 and I encountered this bug . 第一次使用Java 8时,我遇到了此错误 。 Anyone encountered this too?
有人也遇到过吗? Bug is marked as resolved however I'm not sure why I'm getting it.
错误被标记为已解决,但是我不确定为什么会得到它。 I also excuted the listed sample code in the bug and did get UOE.
我还摘录了错误中列出的示例代码,并获得了UOE。 Here's my environment:
这是我的环境:
OS in VM x64 VM x64中的操作系统
$ cat /etc/lsb-release
DISTRIB_ID="elementary OS"
DISTRIB_RELEASE=0.2.1
DISTRIB_CODENAME=luna
DISTRIB_DESCRIPTION="elementary OS Luna"
JDK JDK
$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
$ javac -version
javac 1.8.0_25
Here's my test 这是我的测试
package test;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
public class RemoveIfTest {
static interface I {
// i want to remove this
public void a();
public String b();
}
@Test(expected = UnsupportedOperationException.class)
public void test() {
List<Method> methods = Arrays.asList(I.class.getMethods());
// what we have
methods.forEach(m -> System.out.println(m));
// output: public abstract void test.RemoveIfTest$I.a()
// public abstract java.lang.String test.RemoveIfTest$I.b()
// old way
for (Method method : methods) {
if (method.getReturnType().equals(Void.TYPE)) {
System.out.println("Remove this > " + method);
}
}
// output: Remove this > public abstract void test.RemoveIfTest$I.a()
// got UOE
methods.removeIf(m -> m.getReturnType().equals(Void.TYPE));
}
}
And here's a screenshot of the evaluated line 这是评估行的屏幕截图
The highlighted line is where it died 高亮的行是它死的地方
As the Javadoc for Arrays.asList
states it is a fixed size list backed by the array: 如
Arrays.asList
的Javadoc所述,它是由数组支持的固定大小的列表:
Returns a fixed-size list backed by the specified array.
返回由指定数组支持的固定大小的列表。 (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray.
(对返回列表的更改为“直写”到数组。)与Collection.toArray结合使用,此方法充当基于数组和基于集合的API之间的桥梁。 The returned list is serializable and implements RandomAccess.
返回的列表是可序列化的,并实现RandomAccess。
Looking at the source for java.util.Arrays
, it returns its own implementation of a List
which is similar to the java.util.ArrayList
. 查看
java.util.Arrays
的源代码,它返回其自己的List
实现,该实现类似于java.util.ArrayList
。
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
...
}
It does not override the remove
method from AbstractList
, which you can see from the source that it throws this exception. 它不会覆盖
AbstractList
的remove
方法,您可以从源代码中看到它引发了此异常。
public E remove(int index) {
throw new UnsupportedOperationException();
}
The solution (like stated already) is to use a list that supports removal. 解决方案(如前所述)是使用支持删除的列表。
List<Method> methods = new ArrayList<>(Arrays.asList(I.class.getMethods()))
Answer from @JBNizet. @JBNizet的回答。 Thank you.
谢谢。
From 从
// List methods = java.util.Arrays.ArrayList
List<Method> methods = Arrays.asList(I.class.getMethods())
To 至
// List methods = java.util.ArrayList
List<Method> methods = new ArrayList<>(Arrays.asList(I.class.getMethods()));
you should check your list,the jdk1.8 describe the throws,the throws's description is "UnsupportedOperationException if elements cannot be removed from this collection. Implementations may throw this exception if a matching element cannot be removed or if, in general, removal is not supported." 您应该检查您的列表,jdk1.8描述了throw,如果无法从此集合中删除元素,则throws的描述为“ UnsupportedOperationException。如果无法删除匹配的元素或通常不能删除,则实现可能会抛出此异常。支持的。” I test this list, it's work.
我测试了这个列表,它是有效的。 this list is as follows:
此列表如下:
List<String> list=new ArrayList<String>();
list.add("1");
list.add("2");
list.removeIf(s->s.equals("1"));
so,good luck. 所以,祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.