I have two array lists,expectedResult and actualResults.

List<String> expectedResult = new ArrayList<String>();
expectedResult.add("Android WarZone");
expectedResult.add("Android Headlines"); 

List<String> actualResult = new ArrayList<String>();
WebElement table = driver1.findElement(By.id("keyword-grid")); 
List<WebElement> allCells = table.findElements(By.tagName("td"));

for (int j=0; j<allCells.size(); j++){

Actual data set is as below.

[Android WarZone, Manual, Active, 2014-11-18 12:21:45, Digitalbox Testing, , Android Headlines, Manual, Active, 2014-11-18 10:25:36, Digitalbox Testing, ]

I am trying to compare the two array list values using below code.

for(int i=0;i<expectedResult.size();i++) {
    if(actualResult.get(i).contains(expectedResult.get(i))) {
        System.out.println("Search function verified");
    }else {
        System.out.println("Search function verification failed"+actualResult.get(i));

below is the output of the comparison.

Search function verification failedAndroid WarZone
Search function verification failedManual
Search function verified
Search function verification failed2014-11-18 12:21:45
Search function verification failedDigitalbox Testing
Search function verification failed

Only one value, Android Headlines, was verified.

How can I compare two array values?

3 Answers 3


You're not checking if the actual array contains the value, you're checking if the element at index i in the actual array matches the value at index i in the array of expected values.

Also, if you're going to iterate over all the values of expectedResult, use the enhanced for loop.

Finally, if you are testing something, use assertions and fail if expected result is not met. From your example it's not clear when to fail. If actual contains value not expected, or if actual does NOT contain a value that is expected?

for (String expected: expectedResult) {
  if (actualResult.contains(expected)) {
    System.out.println("Search function verified");
  } else {
    System.out.println("Search function verification failed" + expectedResult.get(i));
  • I still get Search function verification failed for date values. Commented Jan 26, 2018 at 10:40
  • Your expected result contains Dates, your actual Result contains DateTime; you'll need to compare them differently as they will not be equal.
    – Marit
    Commented Jan 26, 2018 at 10:51
  • Also, when should your test fail? Should it fail if expected and actual are not exactly the same? or when actual does not contain all expected? or when expected does not contain all actual?
    – Marit
    Commented Jan 26, 2018 at 10:51
  • It should fail when actual does not contain all expected. Thank you. Commented Jan 26, 2018 at 10:53
  • ok, then iterate over all expected like in the example
    – Marit
    Commented Jan 26, 2018 at 10:55

If you're looking for a pure Java solution, it all depends on how confident you want to be that they're the same.

The easiest way to do it is to leverage the collections API who did all this work for you, which is simply:

boolean testPassed = expectedResult.containsAll(actualResult);

This will be quite slow on an ArrayList in terms of order, but for such small values that isn't going to be a problem.

For larger lists, you can use a set to make the lookups faster at the expense of a bigger memory footprint and slower startup. You probably wouldn't notice a difference until a few thousand records (if you notice at all) but some tests can have very large sets, so... why not!

Set<String> expectedSet = new HashSet<>(expectedValue);
Set<String> actualSet = new HashSet<>(actualValue);
boolean testPassed = expectedSet.containsAll(actualSet);

If you're concerned about duplicates, you can take a slightly longer approach by sorting the values and walking down the arrays. Notice how as soon as it becomes false, it stops walking down the array.

boolean testPassedSoFar = expectedList.size() == actualList.size();
if(testPassedSoFar) {
    for(int i = 0; i < expectedList.size() && testPassedSoFar; i++) {
        testPassedSoFar &= expectedList.get(i).equals(actualList.get(i));

You're checking the content of the String's underlying char array (i. e. String#contains), but what you want is List#contains.

However, don't reinvent the wheel, use existing libraries. You can use JUnit to better manage your Selenium tests and an assertion library such as AssertJ. Using the latter, all you have to do is:


You can also use containsExactly if you care about the order.

Don't worry too much about performance/complexity of these operations, the most important thing is that the tests are readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.