Remove elements from ArrayList

2020-07-29 17:52发布

I have to remove elements from ArrayList, but I have not gone through it. Elements which I have to remove are also available in ArrayList. In short, I have to remove one Array List from another Array List. e.g. Suppose

ArrayList<String> arr1= new ArrayList<String>();
ArrayList<String> arr2 = new ArrayList<String>();
arr1.add("1"); 
arr1.add("2"); 
arr1.add("3"); 

arr2.add("2"); 
arr2.add("4"); 

Now, I have to remove elements which are in arr2 from arr1. So, that I have final answer as 1 and 3. What needs to be done?

5条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-07-29 18:07

To remove duplicate of one from other use this

    int arr1Size = arr2.size();
    int arr2Size = arr2.size();
    for (int i = 0; i < arr1Size; i++)
    {
        for (int j = 0; j < arr2Size; j++)
        {
            if (arr1.get(i).contains(arr2.get(j)))
            {
                arr1.remove(i);
            }
        }
    }
    System.out.print(arr1);
查看更多
Lonely孤独者°
3楼-- · 2020-07-29 18:07

Ok to make things clear:

if your list is composed of basic elements such as String etc all you need to do is use

list2.removeAll(list1);

assuming that isnt the case meaning you created a list from custum objects - the above method wont work, that is due to the nature of the item comparison. it uses the object.equals method which by default checks if this is the same instance of the object in the other list (which it probably isnt)

so in order for this to work you need to overwrite the custom object equals method.

example - test if 2 contacts are the same based on phone number:

public boolean equals(Object o) 
    {
        if (o==null)
        {
            return false;
        }

        if (o.getClass()!=this.getClass())
        {
            return false;
        }

        Contact c=(Contact)o;
        if (c.get_phoneNumber().equals(get_phoneNumber()))
        {
            return true;    
        }

        return false;
    }

now if you use

list2.removeAll(list1);

it will compare the items based on the desired attribute (in the example based on phone number) and will work as planned.

查看更多
时光不老,我们不散
4楼-- · 2020-07-29 18:14

Read Remove Common Elements in Two Lists Java


Use below code

List<String> resultArrayList = new ArrayList<String>(arr1);
resultArrayList.removeAll(arr2);

Or can be done by

arr1.removeAll(arr2)

After SO comments

I used the following code

ArrayList<String> arr1= new ArrayList<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("1"); 
        arr1.add("2"); 
        arr1.add("3"); 

        arr2.add("2"); 
        arr2.add("4"); 

        System.out.println("Before removing---");
        System.out.println("Array1 : " + arr1);
        System.out.println("Array2 : " + arr2);
        System.out.println("Removing common ---");
        List<String> resultArrayList = new ArrayList<String>(arr1);
        resultArrayList.removeAll(arr2);                
        System.out.println(resultArrayList);

and getting output as

Before removing---
Array1 : [1, 2, 3]
Array2 : [2, 4]
Removing common ---
[1, 3]

So what is not working at your side?

Read more about How do you remove the overlapping contents of one List from another List?

查看更多
家丑人穷心不美
5楼-- · 2020-07-29 18:19

You can use removeAll() function

/**
 * Removes from this list all of its elements that are contained in the
 * specified collection.
 *
 * @param c collection containing elements to be removed from this list
 * @return {@code true} if this list changed as a result of the call
 * @throws ClassCastException if the class of an element of this list
 *         is incompatible with the specified collection
 * (<a href="Collection.html#optional-restrictions">optional</a>)
 * @throws NullPointerException if this list contains a null element and the
 *         specified collection does not permit null elements
 * (<a href="Collection.html#optional-restrictions">optional</a>),
 *         or if the specified collection is null
 * @see Collection#contains(Object)
 */
public boolean removeAll(Collection<?> c) {
    return batchRemove(c, false);
}
查看更多
Bombasti
6楼-- · 2020-07-29 18:21

Take new arr as final sorted array

for(int i=0;i<arr1.size();i++)
    {
    for(int j=0;j<arr2.size();j++)
    if(!arr1.get(i).contains(arr2.get(j)))
    {
    arr.add(arr1.get(i));
    }
    }
查看更多
登录 后发表回答