Comparing two lists to calculate the difference(s) - Android / Java

298 Views Asked by At

I have two lists - one which returns a list of all folders and one which returns a list of recent folders. What I’d like to do is compare each list to each other - and if a match is found: display a toast.

My current code is as follows:

    boolean currentFolderFound = false;
            if (mAllFolderListCursor != null) {
                final String folderName = mSelectedFolderUri.toString();
                if (mAllFolderListCursor.moveToFirst()) {
                    do {
                        final Folder f = mAllFolderListCursor.getModel();
                        if (!isFolderTypeExcluded(f)) {
                            if (f.folderUri.equals(mRecentFolders)) {
                                Toast.makeText(getApplicationContext(), "MATCH FOUND",
                                           Toast.LENGTH_LONG).show();
                            }
                        }
                    } while (!currentFolderFound && mAllFolderListCursor.moveToNext());
                }
}

I believe I’ve coded the line: if (f.folderUri.equals(mRecentFolders)) { incorrectly - and I’m wondering how this might be improved to provide the result I am looking for.

Thanks in advance,

Christopher

1

There are 1 best solutions below

2
On BEST ANSWER

equals returns true if the objects are actually equal, but in your case they are not. You should use a simple string comparison using compareTo method instead.

String f1 = folder1.folderUri.toString();
String f2 = folder2.folderUri.toString();
if (f1.compareTo(f2) == 0) { // Do what needs to be done}