How to remove element from ArrayList by checking its value?

后端 未结 11 1144
一整个雨季
一整个雨季 2020-12-01 03:47

I have ArrayList, from which I want to remove an element which has particular value...

for eg.

ArrayList a=new ArrayList         


        
相关标签:
11条回答
  • 2020-12-01 03:51

    In your case, there's no need to iterate through the list, because you know which object to delete. You have several options. First you can remove the object by index (so if you know, that the object is the second list element):

     a.remove(1);       // indexes are zero-based
    

    Then, you can remove the first occurence of your string:

     a.remove("acbd");  // removes the first String object that is equal to the
                        // String represented by this literal
    

    Or, remove all strings with a certain value:

     while(a.remove("acbd")) {}
    

    It's a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can't remove them by using remove with an object that is equal to the one you want to delete.

    In those case, I usually use a second list to collect all instances that I want to delete and remove them in a second pass:

     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);
     }
    
    0 讨论(0)
  • 2020-12-01 03:54

    Snippet to remove a element from any arraylist based on the matching condition is as follows:

    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();
            }
        }
    
    0 讨论(0)
  • 2020-12-01 03:58

    This will give you the output,

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

    output,

    [c, d, e, f, g]
    
    0 讨论(0)
  • 2020-12-01 03:59

    for java8 we can simply use removeIf function like this

    listValues.removeIf(value -> value.type == "Deleted");
    
    0 讨论(0)
  • 2020-12-01 03:59

    Try below code :

     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); 
    }
    

    your output :

     [abc, xyz, test, test123]
    abc
    xyz
    test
    test123
    [abc, xyz, test123]
    
    0 讨论(0)
  • 2020-12-01 04:03

    Just use myList.remove(myObject).

    It uses the equals method of the class. See http://docs.oracle.com/javase/6/docs/api/java/util/List.html#remove(java.lang.Object)

    BTW, if you have more complex things to do, you should check out the guava library that has dozen of utility to do that with predicates and so on.

    0 讨论(0)
提交回复
热议问题