是什么导致了这个retainAll异常?
java.lang.UnsupportedOperationException: This operation is not supported on Query Results
at org.datanucleus.store.query.AbstractQueryResult.contains(AbstractQueryResult.java:250)
at java.util.AbstractCollection.retainAll(AbstractCollection.java:369)
at namespace.MyServlet.doGet(MyServlet.java:101)
我试图获取从数据存储查询中检索到的一个列表,并仅保留从键列表中检索到的列表中的结果。我的两个列表都按预期填充,但我似乎无法在其中任何一个上使用retainAll。
// List<Data> listOne = new ArrayList(query.execute(theQuery));
// DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
// List<Data> listTwo = new ArrayList(ds.get(keys).values());
// listOne.retainAll(listTwo);
编辑
好吧,为了简化,因为这显然是多个问题合而为一,我已经停止使用数据存储的低级 API,而不是只是用循环一一拉动。
List<MyClass> test = (List<MyClass>) query.execute();
List<MyClass> test2 = new ArrayList<MyClass>();
for (String key : favorites) {
test2.add(pm.getObjectById(MyClass.class, key));
}
log.info(test.toString());
test.retainAll(test2);
以上有效。它不会抛出异常。下面确实抛出异常。唯一的区别是 log.info。我很困惑。
List<MyClass> test = (List<MyClass>) query.execute();
List<MyClass> test2 = new ArrayList<MyClass>();
for (String key : favorites) {
test2.add(pm.getObjectById(MyClass.class, key));
}
test.retainAll(test2);
java.lang.UnsupportedOperationException: This operation is not supported on Query Results
at org.datanucleus.store.query.AbstractQueryResult.contains(AbstractQueryResult.java:250)
at java.util.AbstractCollection.retainAll(AbstractCollection.java:369)
at namespace.MyServlet.doGet(MyServlet.java:101)
I'm attempting to take one list I retrieved from a datastore query, and keep only the results which are also in a list I retrieved from a list of keys. Both my lists are populated as expected, but I can't seem to user retainAll on either one of them.
// List<Data> listOne = new ArrayList(query.execute(theQuery));
// DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
// List<Data> listTwo = new ArrayList(ds.get(keys).values());
// listOne.retainAll(listTwo);
EDIT
Ok, in an attempt to simplify, since this is apparently multiple problems in one, I have stopped using the low level API for datastore and instead of am just pulling one by one with a loop.
List<MyClass> test = (List<MyClass>) query.execute();
List<MyClass> test2 = new ArrayList<MyClass>();
for (String key : favorites) {
test2.add(pm.getObjectById(MyClass.class, key));
}
log.info(test.toString());
test.retainAll(test2);
The above works. It doesn't throw the exception. The below does throw the exception. The only difference is the log.info. I'm stumped.
List<MyClass> test = (List<MyClass>) query.execute();
List<MyClass> test2 = new ArrayList<MyClass>();
for (String key : favorites) {
test2.add(pm.getObjectById(MyClass.class, key));
}
test.retainAll(test2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
但是,您需要将它们放入
new ArrayList()
中。返回的List
实现显然不支持retainAll()
。这就是例外告诉你的。“普通”
ArrayList
支持它。如果由于泛型类型的差异而无法通过 ArrayList 构造函数,那么您需要手动循环它并在添加之前强制转换每个项目。更新:根据您的更新,实体显然是延迟加载/填充的。大多数 ORM(DataNucleus 就是其中之一)确实可以这样做。由于我不使用 DataNucleus,因此我无法详细说明如何以“好的”方式修复该问题。但你至少现在知道了问题的根本原因,并且可以按照上面的方法解决。同样在循环中填充列表
test
。You however need to put them in a
new ArrayList()
. The returnedList
implementation apparently doesn't supportretainAll()
. That's what the exception is telling you.A "plain"
ArrayList
supports it. If passing through theArrayList
constructor is not possible due to difference in generic type, then you'll need to manually loop over it and cast each item before adding.Update: as per your update, the entities are apparently lazily loaded/filled. Most ORM's (DataNucleus is one) may indeed do that. As I don't use DataNucleus, I can't go in detail how to fix that in a "nice" way. But you at least now know the root cause of the problem and it can be solved the same way as above. Fill the list
test
in a loop as well.如果您用于“键列表”的集合类型不支持
retainAll
,则会抛出异常。您使用哪种类型?If the type of collection you use for your "list of keys" does not support
retainAll
that exception will be thrown. Which type are you using?提示:您不需要迭代来填充 listTwo。
只需执行:
listTwo.addAll(ds.get(keys).values())
TIP: you don't need to iterate to fill listTwo.
just do:
listTwo.addAll(ds.get(keys).values())