Learn to compare two arraylists in Java with simple examples. We will first test if two arraylists are equal or not. If both lists are not equal, we will find the difference between lists.
The difference in list is equals to another third list which contains either additional elements or missing elements.
Also learn to find common elements between two arraylists.
1. Compare two arraylists for equality
Java program to test if two given lists are equal. To test equality –
- Sort both lists.
- Compare both lists using equals() method.
List.equals()
method return true if both elements are of same size and both contains same set of elements in exactly same order.
public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); Collections.sort(listOne); Collections.sort(listTwo); //Compare unequal lists example boolean isEqual = listOne.equals(listTwo); //false System.out.println(isEqual); //Compare equals lists example ArrayList<String> listThree = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); isEqual = listOne.equals(listThree); //true System.out.println(isEqual); } }
Program output.
false true
2. Compare two arraylists – find additional elements
If two arraylists are not equal and we want to find what are additional elements in first list in comparison to second list then we can use this method.
It uses removeAll() method which removes all elements of second list from first list. It leaves only additonal elements in first list.
public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); //remove all elements of second list listOne.removeAll(listTwo); System.out.println(listOne); } }
Program output.
[f]
3. Compare two arraylists – find missing elements
To get the missing elements in first list, which are present in second list, we can reverse the above example. Here we can remove all elements of first list from second list using removeAll() method.
public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); //remove all elements from second list listTwo.removeAll(listOne); System.out.println(listTwo); } }
Program output.
[e]
4. Compare two arraylists – find common elements
To find common elements in two arraylists, use List.retainAll() method. This method retains only the elements in this list that are contained in the specified arraylist passed as method argument.
public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); listOne.retainAll(listTwo); System.out.println(listOne); } }
Program output.
[a, b, c, d]
Above example will work good in Java 8 as well.
Happy Learning !!
Read More:
A Guide to Java ArrayList
ArrayList Java Docs
Hi Lokesh,
For finding the common elements, does retainAll() method goes thru the entire list? Is this the best approach to find the common elements or is there any other approach which is better than retainAll() and contains()?
Thank you,
bkk
I have 2 arraylist to compare and pass if values are matching. One of the arraylist contains the values with (some text), once i remove the extra content, i can compare using your method. please help.
How do i remove the particular content from arraylist before comparing. Below is my code:
[java]List PMPageCMList = driver.findElements(By.xpath(“//div[@id=’collapseCM’]/div[2]/div[2]/div”));
int totalcms = PMPageCMList.size();
for(int i=1;i<=totalcms;i++){ CaseManagersreceivingreminders.add( driver.findElement( By.xpath("//div[@id='collapseCM']/div[2]/div[2]/div["+i+"]/span")).getText()); }[java] Expected result: [ Ranjit Nayak , ranjti Nyak , shenoy attorney ] Actual result using my code: [ Ranjit Nayak (HR), ranjti Nyak (Parelegal), shenoy attorney (attorney) ]
Hi Ranjit, how could I possibly know without looking at the data you are reading? Please break the code in multiple lines. Execute one method at a time and assign the result to a variable. You are doing so much in single line.
Then put breakpoints and debug.
create a list with cases sensitive characters and special characters and try to sort the list ascending and descending
for me ascending works with case sensitive option
but descending does not
any help please ?
Really good example with good explanation…
Good example