如何从 Java 集合中弹出项目?
JDK 或 apache commons 中是否有一种方法可以从 java.util.List 中“弹出”元素列表?我的意思是,删除元素列表并返回它,就像这个方法一样:
public Collection pop(Collection elementsToPop, Collection elements) {
Collection popped = new ArrayList();
for (Object object : elementsToPop) {
if (elements.contains(object)) {
elements.remove(object);
popped.add(object);
}
}
return popped;
}
Is there a method in JDK or apache commons to "pop" a list of elements from a java.util.List? I mean, remove the list of elements and return it, like this method:
public Collection pop(Collection elementsToPop, Collection elements) {
Collection popped = new ArrayList();
for (Object object : elementsToPop) {
if (elements.contains(object)) {
elements.remove(object);
popped.add(object);
}
}
return popped;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您正在寻找类似堆栈的结构,我建议接受
Deque
(LinkedList
是最常见的实现)而不是Collection
。如果您实际上不需要将其视为堆栈,只需从
Collection
中获取迭代器并使用remove()
方法:请注意,remove 是可选的操作,并且某些实现可能会抛出
UnsupportedOperationException
(例如,Collections.unmodifying...() 中的 Collection 返回的迭代器将会抛出)。编辑:在更仔细地研究你的问题后,我认为你只需要这个:
如果你的主要观点是你需要确切地知道哪些元素实际上被弹出,我认为你被你原来的代码困住了。
If you're looking for a stack-like structure I suggest accepting a
Deque
(LinkedList
is the most common implementation) instead of aCollection
.If you don't actually need to treat it as a stack, just get an iterator from the
Collection
and use theremove()
method:Do note that remove is an optional operation, and some implementations may throw an
UnsupportedOperationException
(for example, the iterator returned by a Collection fromCollections.unmodifiable...()
will).Edit: After looking more closely at your question, I think you just need this:
If your main point is you need to know exactly which elements were actually popped, I think you're stuck with your original code.
JDK提供的标准方法中没有这样的方法。 Apache Commons 提供
ListUtils.subtract()
方法。编辑:正如其他回答者所指出的,您对术语
pop
的使用是不标准的。通常,维基百科有一个很好的描述堆栈。
There is no such method in the standard JDK-provided methods. Apache Commons provides the
ListUtils.subtract()
method.Edit: As other answerers have noted, your use of the term
pop
is nonstandard. Usually,Wikipedia has a nice description of stacks.
我想不是,因为你对“pop”操作的定义非常不标准。通常它不带任何参数(集合本身除外)并返回并删除最顶层的参数。
但是一旦您注意到 apache commons,这将达到与您的代码相同的效果。
编辑
http://commons.apache.org/collections/api-release/index.html html
I guess no, because you definition of 'pop' operation is highly non-standard. Usually it takes no arguments (except collection itself) and returns and removes the top-most one.
But once you noted apache commons, this would achieve the same effect as your code.
edit
http://commons.apache.org/collections/api-release/index.html
链表提供了您需要的功能,提供了push和pop方法。
请参阅所提供的文档:
Linked List provides the functionality as you require, provides a push and pop method.
Refer to the documentation as provided:
没有一种方法与您所要求的完全一样,但看起来您已经非常接近您的代码了。
一些建议:
如果 elements 是任意集合,请考虑使用removeAll(object) 而不是remove(object),因为您可能需要删除重复项,例如,如果elements 是列表。
contains() 对于某些集合类型(例如列表)来说很慢,因为它需要遍历整个数据结构。鉴于这是在您的内部循环中,您面临 O(n^2) 性能问题的风险。如果您可以使算法与 HashSet 或 HashMap 一起使用,那么 contains() 的复杂度将是 O(1),并且您的算法将更加高效。
There isn't a method exactly like what you are asking for, but it looks like you are already pretty close with your code.
Some suggestions:
Consider using removeAll(object) instead of remove(object) if elements is an arbitrary collection since you may need to remove duplicates e.g. if elements is a list.
contains() is slow for some collection types (e.g. lists) since it needs to traverse the entire data structure. Given that this is in your inner loop you are at risk of O(n^2) performance issues. If you can make the algorithm work with a HashSet or HashMap then contains() will by O(1) and your algorithm will be much more efficient.