如何从 Java 集合中弹出项目?

发布于 2024-09-04 08:00:07 字数 393 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

勿忘初心 2024-09-11 08:00:07

如果您正在寻找类似堆栈的结构,我建议接受 DequeLinkedList 是最常见的实现)而不是 Collection

如果您实际上不需要将其视为堆栈,只需从 Collection 中获取迭代器并使用 remove() 方法:

for (Iterator<SomeType> it = elements.iterator(); it.hasNext(); ) {
    SomeType e = it.next();
    it.remove();
    popped.add(e);
}

请注意,remove 是可选的操作,并且某些实现可能会抛出 UnsupportedOperationException (例如,Collections.unmodifying...() 中的 Collection 返回的迭代器将会抛出)。

编辑:在更仔细地研究你的问题后,我认为你只需要这个:

elements.removeAll(elementsToRemove);

如果你的主要观点是你需要确切地知道哪些元素实际上被弹出,我认为你被你原来的代码困住了。

If you're looking for a stack-like structure I suggest accepting a Deque (LinkedList is the most common implementation) instead of a 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:

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).

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 which elements were actually popped, I think you're stuck with your original code.

肩上的翅膀 2024-09-11 08:00:07

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,

The pop operation removes an item from the top of [a stack]

Wikipedia has a nice description of stacks.

没︽人懂的悲伤 2024-09-11 08:00:07

我想不是,因为你对“pop”操作的定义非常不标准。通常它不带任何参数(集合本身除外)并返回并删除最顶层的参数。

但是一旦您注意到 apache commons,这将达到与您的代码相同的效果。

Collection result = CollectionUtils.intersection(a, b);
a.removeAll(b);

编辑
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.

Collection result = CollectionUtils.intersection(a, b);
a.removeAll(b);

edit
http://commons.apache.org/collections/api-release/index.html

野侃 2024-09-11 08:00:07

链表提供了您需要的功能,提供了push和pop方法。

请参阅所提供的文档

Linked List provides the functionality as you require, provides a push and pop method.

Refer to the documentation as provided:

皓月长歌 2024-09-11 08:00:07

没有一种方法与您所要求的完全一样,但看起来您已经非常接近您的代码了。

一些建议:

  • 如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文