In this tutorial we will learn how to compare two ArrayList. We would be using contains[] method for comparing two elements of different ArrayList.
public boolean contains[Object o]
It returns true if the list contains the Object o else it returns false.
Example:
In this example we have two ArrayListal1 and al2 of String type. We have compared these ArrayLists using contains[] method and stored the comparison result in third ArrayList [al3 and al4].
Output:
[No, No, No, Yes, Yes] [0, 0, 0, 1, 1]What is the logic in above code?
If the first element of ArrayList al1 is present in al2 then ArrayList al3 would be having Yes and al4 would be having 1 However if the element is not present No would be stored in al3 and 0 would be in al4.