I have done something like this :
public class MainActivity extends AppCompatActivity { ArrayList<String> al = new ArrayList<String>(); ArrayList<String> a2 = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); a2.add("C"); a2.add("C"); a2.add("E"); a2.add("B"); a2.add("D"); a2.add("F"); for (int i = 0; i < al.size(); i++) { for (int j = 0; j < a2.size(); j++) { if (al.get(i).equals(a2.get(j))) { a2.remove(j); Log.e("array 2 ", a2.toString()); break; } } } } }
-- But my output is like this :
[C, E, B, D, F]
[C, B, D, F]
[C, D, F]
[C, F]
[C]
-- i am having same elements in arraylist 2, then also i am getting 'C', it should be null .i.e zero un-common value.
al.removeAll(a2)removes the common items betweenalanda2. Read more here