Java:为什么 subList(0, 5).clear() 对我的对象不起作用?
例如,如果我在 List
上运行此操作,它会按预期工作(删除前 5 个元素),但是当我在对象列表上运行它时,什么也不会发生(列表保持不变) )。
list.subList(0, 5).clear();
我的类是一个 pojo,它没有实现 equals
或 hashCode
(如果有的话)。
更新: 我使用的实现是 ArrayList,它是从 Hibernate 查询返回的。真的没有什么可展示的。子列表不返回空列表。
对于那些不相信它适用于整数列表的人来说,这是一个示例:
List<Integer> testList = new ArrayList<Integer>();
for(int i=0;i<10;i++) {
testList.add(i);
}
testList.subList(0, 5).clear();
for(int i=0;i<testList.size();i++) {
System.out.print(testList.get(i)+" ");
}
结果是 5 6 7 8 9
UPDATE2: 实际上一切都按预期工作,不知道我怎么看不到这一点(对结果的数量感到困惑)。抱歉误报:) 这个问题可以删除。
If I run this operation on List<Integer>
for example, it works as expected (removes first 5 elements), but when I run it on a list of my objects, nothing happens (list stays the same).
list.subList(0, 5).clear();
My class is a pojo that doesn't implement equals
or hashCode
, if that matters.
UPDATE:
The implementation I am using is ArrayList, which is returned from Hibernate query. There is nothing to show, really. Sublist doesn't return an empty list.
Here is an example for those who don't beleive it works on a list of Integers:
List<Integer> testList = new ArrayList<Integer>();
for(int i=0;i<10;i++) {
testList.add(i);
}
testList.subList(0, 5).clear();
for(int i=0;i<testList.size();i++) {
System.out.print(testList.get(i)+" ");
}
The result is 5 6 7 8 9
UPDATE2: Actually everything is working as expected, don't know how I couldn't see that (got confused by numbers of results). Sorry for false alarm :) This question could be deleted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它在我的机器上工作tm
产生:
所以,问题不在于
ArrayList
也不在于您的对象。我不认为
Hibernate
返回一个普通的旧ArrayList
(可能是这样)尝试打印
并让我们知道它是否实际上是一个
ArrayList
>It works on my machinetm
Produces:
So, the problem is not in
ArrayList
nor in your objects.I don't think
Hibernate
returns a plain oldArrayList
( may be it does )Try printing
And let us know if it is in fact an
ArrayList
编辑 - 看起来我错了,但还是把我原来的答案留在这里:
你确定它适用于
List
吗?不应该。subList()
方法返回一个单独的List
。如果从该列表中删除元素,它不会影响原始列表。List.subList()
的 API 文档是这样说的:清理清单并不是非结构性变化;而是非结构性变化。仅更改列表中的元素是非结构性更改。
这与您的 POJO 是否具有 equals 或 hashCode 方法无关。
编辑 - 我刚刚使用
ArrayList
进行了尝试,它确实有效(不仅适用于Integer
,还使用我自己的对象作为列表元素)。edit - Looks like I was wrong, but leaving my original answer here anyway:
Are you sure that it works with a
List<Integer>
? It shouldn't.The method
subList()
returns a separateList
. If you remove elements from that list, it shouldn't affect the original list. The API docs forList.subList()
say this:Clearing a list is not a non-structural change; only changing the elements in the list are non-structural changes.
This has nothing to do with whether your POJO has
equals
orhashCode
methods or not.edit - I just tried it out with an
ArrayList
and it does work (not only withInteger
, but also with my own object as a list element).我能想到的两件事是:
list.sublist(0, 5)
返回一个空列表,因此.clear()
不执行任何操作。不确定您正在使用的 List 实现(ArrayList、LinkedList 等)的内部工作原理,但实现
equals
和hashCode
可能很重要。我在地图方面遇到了类似的问题,其中 HashMap 肯定需要 hashCode 实现。
Two things I can think of are:
list.sublist(0, 5)
returns an empty list, therefore.clear()
does nothing.Not sure of the inner workings of the List implementation you're using (ArrayList, LinkedList, etc), but having the
equals
andhashCode
implemented may be important.I had a simiarl issue with Maps, where HashMap definitely needs the hashCode implementation.
您是否尝试过手动创建对象的
List
并执行相同的操作(不使用 Hibernate)?在我看来,这可能与 Hibernate 的数据延迟加载有关...如果您尚未读取返回的List
中的数据,则它可能尚未加载(因为子列表本身)只是视图)。在这种情况下,clear
可能什么也不做。Have you tried creating a
List
of your objects manually and doing the same thing (without Hibernate)? It seems possible to me that this has to do with Hibernate's lazy loading of data... if you haven't read the data in the returnedList
, it may not have been loaded yet (since sublists themselves are just views). In that case, it's possibleclear
would do nothing.是否有可能从 Hibernate 返回的 List 不可修改?即由 Collections.unmodifyingList() 包装
Is it possible that the List returned from Hibernate is not modifiable? i.e. wrapped by Collections.unmodifiableList()