如何从Java集合中弹出项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2998848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pop items from a collection in Java?
提问by Tom Brito
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:
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;
}
采纳答案by Mark Peters
If you're looking for a stack-like structure I suggest accepting a Deque
(LinkedList
is the most common implementation) instead of a Collection
.
如果您正在寻找类似堆栈的结构,我建议接受 a Deque
(LinkedList
是最常见的实现) 而不是Collection
.
If you don't actually need to treat it as a stack, just get an iterator from the Collection
and use the remove()
method:
如果您实际上不需要将其视为堆栈,只需从 中获取迭代器Collection
并使用该remove()
方法:
for (Iterator<SomeType> it = elements.iterator(); it.hasNext(); ) {
SomeType e = it.next();
it.remove();
popped.add(e);
}
Do note that remove is an optional operation, and some implementations may throw an UnsupportedOperationException
(for example, the iterator returned by a Collection from Collections.unmodifiable...()
will).
请注意,remove 是一个可选操作,某些实现可能会抛出一个UnsupportedOperationException
(例如,来自Collections.unmodifiable...()
will的 Collection 返回的迭代器)。
Edit: After looking more closely at your question, I think you just need this:
编辑:在更仔细地查看您的问题后,我认为您只需要这个:
elements.removeAll(elementsToRemove);
If your main point is you need to know exactly whichelements were actually popped, I think you're stuck with your original code.
如果您的主要观点是您需要确切地知道实际弹出了哪些元素,我认为您被困在原始代码中。
回答by Nikita Rybak
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.
但是一旦您注意到 apache commons,这将达到与您的代码相同的效果。
Collection result = CollectionUtils.intersection(a, b);
a.removeAll(b);
edit
http://commons.apache.org/collections/api-release/index.html
编辑
http://commons.apache.org/collections/api-release/index.html
回答by mikera
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.
如果元素是任意集合,请考虑使用 removeAll(object) 而不是 remove(object),因为您可能需要删除重复项,例如,如果元素是列表。
contains() 对于某些集合类型(例如列表)来说很慢,因为它需要遍历整个数据结构。鉴于这是在您的内部循环中,您面临 O(n^2) 性能问题的风险。如果您可以使算法与 HashSet 或 HashMap 一起工作,那么 contains() 将通过 O(1) 并且您的算法将效率更高。
回答by Pops
There is no such method in the standard JDK-provided methods. Apache Commons provides the ListUtils.subtract()
method.
在标准的 JDK 提供的方法中没有这样的方法。Apache Commons 提供了该ListUtils.subtract()
方法。
Edit: As other answerers have noted, your use of the term pop
is nonstandard. Usually,
编辑:正如其他回答者所指出的,您对该术语的使用pop
是非标准的。通常,
The pop operation removes an item from the top of [a stack]
pop 操作从 [a stack] 的顶部移除一个项目
Wikipedia has a nice description of stacks.
回答by Alexander M
Linked List provides the functionality as you require, provides a push and pop method.
链表提供您需要的功能,提供推送和弹出方法。
Refer to the documentationas provided:
请参阅提供的文档: