java 使用某些 Object 属性从 ArrayList 中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36028995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:53:10  来源:igfitidea点击:

Remove object from ArrayList with some Object property

javaloopsobjectarraylist

提问by django

I am maintaining one ArrayListof objects. And my object structure is Id, name, some other details. I need to remove one the object with some id value say(10) and I don't want to iterate over the list. Is there any solution for this?

我正在维护一个ArrayList对象。我的对象结构是 ID、名称和其他一些细节。我需要删除一个具有一些 id 值 say(10) 的对象,我不想遍历列表。有什么解决办法吗?

回答by Tagir Valeev

Using Java-8 Collection#removeIf

使用 Java-8 Collection#removeIf

myList.removeIf(obj -> obj.id == 10);

With Java-7 you'll have to use iterator:

使用 Java-7,您必须使用迭代器:

for(Iterator<MyType> iterator = myList.iterator(); iterator.hasNext(); ) {
    if(iterator.next().id == 10)
        iterator.remove();
}

Note that list iteration is necessary in any case. In Java-8 removeIfmethod it's just performed internally.

请注意,在任何情况下都需要列表迭代。在 Java-8removeIf方法中,它只是在内部执行。

回答by Stephen C

It is not possible1to remove instances of an element from an ArrayListwithout iterating the list in some way2. The ArrayListis an array under the hood, and you need to examine each element in the array to see whether it matches the criteria for removal. At the fundamental level, that entails a loop ... to iterate over the elements.

不可能1从 中删除元素的实例ArrayList而不以某种方式迭代列表2。这ArrayList是一个底层的数组,您需要检查数组中的每个元素以查看它是否符合删除条件。在基本层面上,这需要一个循环......来迭代元素。

Also note that when you remove a single element from an array, all elements with positions after the removed elements need to be moved. On average, that will be half of the array elements.

另请注意,当您从数组中删除单个元素时,需要移动位置在被删除元素之后的所有元素。平均而言,这将是数组元素的一半。

Now, you can code these operations in ways that avoid you using an explicit forloop, but the iteration will be happening behind the scenes, no matter how you code it.

现在,您可以通过避免使用显式for循环的方式对这些操作进行编码,但是无论您如何编码,迭代都将在幕后进行。



1 - Not strictly true. Hypothetically, if you had a separate data structure that (for instance) mapped values to the indexesof elements in the ArrayList, then you could remove the elements without iterating. But I can't see how you could manage that data structure efficiently.

1 - 不完全正确。假设,如果您有一个单独的数据结构(例如)将值映射到中元素的索引ArrayList,那么您可以在不迭代的情况下删除元素。但是我看不出您如何有效地管理该数据结构。

2 - Iteration doesn't just mean using an Iterator. For loops, Stream, Collections.removeIfand other solutions all entail iterating the elements of the list under the hood.

2 - 迭代不仅仅意味着使用Iterator. For循环,StreamCollections.removeIf和其他解决方案都继承权迭代引擎盖下的列表中的元素。

回答by Rayyan

You could not do that without iterator, you should you Hashmap for this.

如果没有迭代器,您就无法做到这一点,您应该为此使用 Hashmap。

public class ObjectStructure{
private int Id;
private String name;
//and any data field you need
}

generate all setters and getters.

生成所有的 setter 和 getter。

Use this class in

在这个类中使用

Hashmap<Integer, ObjectStructure> data = new HashMap<>();

you can add and delete data with only key which is Integer.

您只能使用整数键添加和删除数据。

data.remove(10);

数据删除(10);

回答by Yassin Hajaj

If you really do not want to iterate over the list, you could use a stream but I personnally prefer Collection#removeIflike @TagirValeev suggested

如果您真的不想遍历列表,则可以使用流,但我个人更Collection#removeIf喜欢@TagirValeev 建议

myList = myList.stream()
               .filter(x -> x.id() != 10)
               .collect(Collectors.toList());