1

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?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Looking Forward
  • 3,579
  • 8
  • 45
  • 65

5 Answers5

11

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?

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • @AnilBhatiya See my updated code snippet and tell me what is not working.. share your code then.. – Pankaj Kumar Aug 21 '13 at 10:29
  • hey, let me tell u, I am getting both Array List from other activity. means array list are coming from One activity say A to second activity say B. – Looking Forward Aug 21 '13 at 10:33
  • And I am getting all elements in resulting array – Looking Forward Aug 21 '13 at 10:34
  • Put logs to make sure that you are getting correct array value. – Pankaj Kumar Aug 21 '13 at 10:35
  • no...still getting the problem. tried different methods to solve, but result is zero... – Looking Forward Aug 21 '13 at 11:44
  • I appreciate ur help and these all answers are known to me actually, but still stuck with the problem. – Looking Forward Aug 21 '13 at 11:46
  • share your code, what you are doing there... given code is working fine. – Pankaj Kumar Aug 21 '13 at 11:46
  • i had the same trouble with an object i created - the problem lies in the way "contained" is working it used object.equals, therefor you need to overite that method so it will mean something with your object, (string equals works fine as is but your objects wont and i assume your code isnt using string and the above is only a demo of what you wanted to achieve) read more on equals here:http://stackoverflow.com/questions/8338326/what-does-equalsobject-obj-do – Gabriel H Dec 03 '14 at 10:56
0

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));
    }
    }
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

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);
}
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

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);
Sarath
  • 21
  • 4
0

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.

Gabriel H
  • 1,558
  • 2
  • 14
  • 35