Java 如何通过检查元素的值从ArrayList中删除元素?

Java 如何通过检查元素的值从ArrayList中删除元素?,java,collections,arraylist,Java,Collections,Arraylist,我有ArrayList,我想从中删除一个具有特定值的元素 例如 ArrayList<String> a=new ArrayList<String>(); a.add("abcd"); a.add("acbd"); a.add("dbca"); ArrayList a=新的ArrayList(); a、 添加(“abcd”); a、 添加(“acbd”); a、 添加(“dbca”); 我知道我们可以迭代arraylist和.remove()方法来删除元素,但我不知道在

我有ArrayList,我想从中删除一个具有特定值的元素

例如

ArrayList<String> a=new ArrayList<String>();
a.add("abcd");
a.add("acbd");
a.add("dbca");
ArrayList a=新的ArrayList();
a、 添加(“abcd”);
a、 添加(“acbd”);
a、 添加(“dbca”);
我知道我们可以迭代arraylist和.remove()方法来删除元素,但我不知道在迭代时如何做。 如何删除值为“acbd”的元素,即第二个元素?

您需要使用类似的:

但这将(因为您没有迭代循环的内容):


如果有更复杂的对象,则需要重写该方法。

只需使用
myList.remove(myObject)

它使用类的equals方法。(见)


顺便说一句,如果您有更复杂的事情要做,您应该查看guava库,该库有十几个用于谓词等的实用程序。

使用列表界面中可用的contains()方法检查列表中是否存在值。如果它包含该元素,获取其索引并删除它

使用迭代器循环遍历列表,然后删除所需的对象

    Iterator itr = a.iterator();
    while(itr.hasNext()){
        if(itr.next().equals("acbd"))
            itr.remove();
    }
你应该检查一下这些问题

您可以使用方法


在您的例子中,不需要遍历列表,因为您知道要删除哪个对象。你有几个选择。首先,您可以按索引删除对象(因此,如果您知道该对象是第二个列表元素):

然后,您可以删除第一次出现的字符串:

 a.remove("acbd");  // removes the first String object that is equal to the
                    // String represented by this literal
或者,删除具有特定值的所有字符串:

 while(a.remove("acbd")) {}
如果您的集合中有更复杂的对象,并且希望删除具有特定属性的实例,那么这就有点复杂了。因此,您无法使用与要删除的对象相同的
remove
来删除它们

在这种情况下,我通常使用第二个列表来收集我要删除的所有实例,并在第二个过程中删除它们:

 List<MyBean> deleteCandidates = new ArrayList<>();
 List<MyBean> myBeans = getThemFromSomewhere();

 // Pass 1 - collect delete candidates
 for (MyBean myBean : myBeans) {
    if (shallBeDeleted(myBean)) {
       deleteCandidates.add(myBean);
    }
 }

 // Pass 2 - delete
 for (MyBean deleteCandidate : deleteCandidates) {
    myBeans.remove(deleteCandidate);
 }
List deletePendidates=new ArrayList();
List myBeans=getThemFromSomewhere();
//通过1-收集和删除候选人
for(MyBean MyBean:myBeans){
如果(应删除(myBean)){
deleteCandidates.add(myBean);
}
}
//通过2-删除
for(MyBean deleteCandidate:deleteCandidates){
myBeans.remove(删除候选项);
}

根据匹配条件从任何arraylist中删除元素的Snippet如下所示:

List<String> nameList = new ArrayList<>();
        nameList.add("Arafath");
        nameList.add("Anjani");
        nameList.add("Rakesh");

Iterator<String> myItr = nameList.iterator();

    while (myItr.hasNext()) {
        String name = myItr.next();
        System.out.println("Next name is: " + name);
        if (name.equalsIgnoreCase("rakesh")) {
            myItr.remove();
        }
    }
List nameList=new ArrayList();
姓名列表。添加(“阿拉法特”);
姓名列表。添加(“Anjani”);
姓名列表。添加(“Rakesh”);
迭代器myItr=nameList.Iterator();
while(myItr.hasNext()){
字符串名称=myItr.next();
System.out.println(“下一个名称是:“+name”);
if(名称.相等信号案例(“rakesh”)){
myItr.remove();
}
}
尝试以下代码:

 public static void main(String[] args) throws Exception{
     List<String> l = new ArrayList<String>();
     l.add("abc");
     l.add("xyz");
     l.add("test");
     l.add("test123");
     System.out.println(l);
     List<String> dl = new ArrayList<String>();
    for (int i = 0; i < l.size(); i++) {
         String a = l.get(i);
         System.out.println(a); 
         if(a.equals("test")){
             dl.add(a);
         }
    }
    l.removeAll(dl);
     System.out.println(l); 
}
一行(java8):


(隐式执行所有迭代)

这将为您提供输出

    ArrayList<String> l= new ArrayList<String>();

    String[] str={"16","b","c","d","e","16","f","g","16","b"};
    ArrayList<String> tempList= new ArrayList<String>();

    for(String s:str){
        l.add(s);
    }

    ArrayList<String> duplicates= new ArrayList<String>();

    for (String dupWord : l) {
        if (!tempList.contains(dupWord)) {
            tempList.add(dupWord);
        }else{
            duplicates.add(dupWord);
        }
    }

    for(String check : duplicates){
        if(tempList.contains(check)){
            tempList.remove(check);
        }
    }

    System.out.println(tempList);

对于java8,我们可以像这样简单地使用removeIf函数

listValues.removeIf(value -> value.type == "Deleted");

您至少可以检查API:appeably duplicate of@AndersR.Bystrup I不想删除重复项SSO您只想删除具有特定值的第一个匹配项?可能的重复项I将使用“abcd”.equals(value)来避免NullPointerException,因为列表可以包含null值。请注意,使用“break”只显式删除值为“abcd”的第一个匹配项。@AdriaanKoster:是的,您是正确的。我已经回答了。此外,关于中断,OP没有提到重复元素的可能性。如果存在重复,则需要删除
中断
。我同意OP没有指定应删除多少实例。我对这个问题加了一条评论。@AdriaanKoster:关于你的第二点,我从来没有涉及过这个问题。做一个
a.remove(新字符串(“acbd”)
行得通吗?@AdriaanKoster:那你怎么做呢?在迭代字符串时,是否对所有字符串进行了实习?。另外,谢谢你的投票;)
removeAll
获取一个集合,因此您需要so
list.removeAll(Arrays.asList(“acbd”)Ooops;)我将改变它。+1,
List
有一个
removeAll(Collection obj)
方法,所以您可以只使用
myBeans.removeAll(delete候选者)而不是遍历
deleteCandidate
list.-1您还没有考虑removeAll的要点,遍历数组和删除看起来很可怕。特别是当您在while语句中有
a.remove
时。一个完整的清晰性练习。@Andreas\D要添加到您的注释中,如果列表中的项目是整数类型,您的删除方法将抛出异常。例如,
Mylist=[2,3,5]
,尝试通过
Mylist.remove(5)
删除
5
。也有助于删除对象,具体取决于以下属性:
list.removeIf(obj->obj.getAttribute().equals(otherObj.getAttribute())
;您可以将其缩短为
list.removeIf(“abcd”::等于)@SamuelPhilipp如果我有对象列表,怎么做?我有list.removeIf(s->“abcd”.equals(s.getName());
 while(a.remove("acbd")) {}
 List<MyBean> deleteCandidates = new ArrayList<>();
 List<MyBean> myBeans = getThemFromSomewhere();

 // Pass 1 - collect delete candidates
 for (MyBean myBean : myBeans) {
    if (shallBeDeleted(myBean)) {
       deleteCandidates.add(myBean);
    }
 }

 // Pass 2 - delete
 for (MyBean deleteCandidate : deleteCandidates) {
    myBeans.remove(deleteCandidate);
 }
List<String> nameList = new ArrayList<>();
        nameList.add("Arafath");
        nameList.add("Anjani");
        nameList.add("Rakesh");

Iterator<String> myItr = nameList.iterator();

    while (myItr.hasNext()) {
        String name = myItr.next();
        System.out.println("Next name is: " + name);
        if (name.equalsIgnoreCase("rakesh")) {
            myItr.remove();
        }
    }
 public static void main(String[] args) throws Exception{
     List<String> l = new ArrayList<String>();
     l.add("abc");
     l.add("xyz");
     l.add("test");
     l.add("test123");
     System.out.println(l);
     List<String> dl = new ArrayList<String>();
    for (int i = 0; i < l.size(); i++) {
         String a = l.get(i);
         System.out.println(a); 
         if(a.equals("test")){
             dl.add(a);
         }
    }
    l.removeAll(dl);
     System.out.println(l); 
}
 [abc, xyz, test, test123]
abc
xyz
test
test123
[abc, xyz, test123]
list.removeIf(s -> s.equals("acbd")); // removes all instances, not just the 1st one
    ArrayList<String> l= new ArrayList<String>();

    String[] str={"16","b","c","d","e","16","f","g","16","b"};
    ArrayList<String> tempList= new ArrayList<String>();

    for(String s:str){
        l.add(s);
    }

    ArrayList<String> duplicates= new ArrayList<String>();

    for (String dupWord : l) {
        if (!tempList.contains(dupWord)) {
            tempList.add(dupWord);
        }else{
            duplicates.add(dupWord);
        }
    }

    for(String check : duplicates){
        if(tempList.contains(check)){
            tempList.remove(check);
        }
    }

    System.out.println(tempList);
[c, d, e, f, g]
listValues.removeIf(value -> value.type == "Deleted");