集合的removeAll方法

发布于 2024-09-05 02:53:25 字数 385 浏览 3 评论 0 原文

我想知道下面这样的事情是否可能,

list.removeAll(namesToRemove)

我希望上下文是可以理解的。

list 的类型为 ArrayList,其中 MyObject 有一个 getName 方法。

namesToRemove 是一个 ArrayList,其中包含要删除的对象的名称。

我知道这可以通过重写 MyObject 类中的 equals 方法来实现。我想知道是否还有其他选择。

I would like to know if something like below is possible,

list.removeAll(namesToRemove)

I hope the context is understandable.

The list is of type ArrayList<MyObject> where MyObject has a getName method.

The namesToRemove is an ArrayList<String> containing names of the objects to be removed.

I know this can be achieved by overriding equals method in MyObject class. I would like to know if any other choice is present.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

拿命拼未来 2024-09-12 02:53:25

您可以使用 Google 收藏集 Collections2.filter()

final List<String> namesToKeep = getNamesToFilter();
List<MyObject> filtered = Collections2.filter(originalList, new Predicate<MyObject>() {
  @Override
  public boolean apply(MyObject o) {
    return namesToKeep.contains(o.getName());
  }
});

You can do something like this using Google Collections Collections2.filter():

final List<String> namesToKeep = getNamesToFilter();
List<MyObject> filtered = Collections2.filter(originalList, new Predicate<MyObject>() {
  @Override
  public boolean apply(MyObject o) {
    return namesToKeep.contains(o.getName());
  }
});
紫﹏色ふ单纯 2024-09-12 02:53:25

Java 8:

list.removeIf(obj -> namesToRemove.contains(obj.getName()));

Java 7 及更早版本:

Iterator<MyObject> iter = list.iterator();
while (iter.hasNext())
    if (namesToRemove.contains(iter.next().getName()))
        iter.remove();

请注意,这两种替代方案都具有二次复杂度。 执行操作使其呈线性,并使用 namesToRemoveSet 而不是 namesToRemove

Set<String> namesToRemoveSet = new HashSet<>(namesToRemove);

您可以通过在代码片段之前

Java 8:

list.removeIf(obj -> namesToRemove.contains(obj.getName()));

Java 7 and older:

Iterator<MyObject> iter = list.iterator();
while (iter.hasNext())
    if (namesToRemove.contains(iter.next().getName()))
        iter.remove();

Note that both alternatives have quadratic complexity. You can make it linear by doing

Set<String> namesToRemoveSet = new HashSet<>(namesToRemove);

before the snippets, and use namesToRemoveSet instead of namesToRemove.

寂寞花火° 2024-09-12 02:53:25

您不想覆盖 Object 类中的任何内容。您需要使用 过滤器实用程序或使用您自己的语义而不是 equals 的集合,可能是 ForwardingCollection 包含在您自己的 等效

所有这一切都可以通过 google guava 来实现,而无需违反任何标准

You don't want to override anything in the Object class. You need to use either a filter utility or a collection that uses your own semantics instead of equals, maybe a ForwardingCollection wrapped around your own implementation of equivalence.

All this can be achieved with google guava without breaking any standards

剩余の解释 2024-09-12 02:53:25

另一种方法:子类 ArrayList 并实现自定义方法,例如:

public class MyArrayList<E> extends ArrayList<E> {

   // all needed constructors

   public void removeAllWithNames(Collection<String> names) {
     // following code is based on aioobe's answer
     Iterator<E> iter = iterator();
       while (iter.hasNext())
         if (names.contains(iter.next().toString()))
           iter.remove();
   }
}

EDIT
更改了代码 - 注释很好,现在自定义列表又是“列表”,但现在我们使用 toString() 方法(为了简单起见)进行过滤。 (可以使用 getName() 方法,但需要更多代码行)

Another approach: Subclass ArrayList and implement a custom method like:

public class MyArrayList<E> extends ArrayList<E> {

   // all needed constructors

   public void removeAllWithNames(Collection<String> names) {
     // following code is based on aioobe's answer
     Iterator<E> iter = iterator();
       while (iter.hasNext())
         if (names.contains(iter.next().toString()))
           iter.remove();
   }
}

EDIT
changed the code - the comment was good, now the custom list is a 'List' again, but now we use the toString() method (for simplicity) for filtering. (using a getName() method is possible but requires more lines of code)

生寂 2024-09-12 02:53:25
Collection<MyObject> list = Collections2.filter(list, mo -> !namesToRemove.contains(mo.getName()));
Collection<MyObject> list = Collections2.filter(list, mo -> !namesToRemove.contains(mo.getName()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文