0

In the below example, how would I go about comparing the contents of ArrayList al4 to either of the other ArrayLists? In the same manner I compared al1 to al2.

import java.util.ArrayList;

public class Details {
    public static void main(String[] args) {
        ArrayList<String> al1 = new ArrayList<String>();
        al1.add("hi");
        al1.add("How are you");
        al1.add("Good Morning");
        al1.add("bye");
        al1.add("Good night");

    ArrayList<String> al2 = new ArrayList<String>();
    al2.add("Howdy");
    al2.add("Good Evening");
    al2.add("bye");
    al2.add("Good night");

    ArrayList<ArrayList<String>> al4 = new ArrayList<ArrayList<String>>();
    al4.add(al1);

    // Storing the comparison output in ArrayList<String>
    ArrayList<String> al3 = new ArrayList<String>();
    for (String temp : al1)
        al3.add(al2.contains(temp) ? "Yes" : "No");
    System.out.println(al3);

    }
}

Output for al3

[No, No, No, Yes, Yes]

2 Answers 2

1

It depends a little on what you mean. You can't directly compare al4 with al2, because they're of different types: one is an ArrayList<String> and one is an ArrayList<ArrayList<String>>.

When you compare al1 and al2, what you're doing is determining, for each element of al1, whether it occurs anywhere in al2. I presume this is what you want. You are not deciding whether it occurs in the same place in al2.

The first thing you should do is improve this comparison, which is rather inefficient. Since you don't care about ordering of al2, you should rewrite the comparison like this:

ArrayList<String> al3 = new ArrayList<String>();
Set<String> al2set = new HashSet<String>(al2);
for (String temp : al1)
    al3.add(al2set.contains(temp) ? "Yes" : "No");

This is a lot more efficient because you don't have to traverse the whole of al2 for each lookup.

Now, if you want to compare your list of lists with a list, I can only assume that you mean you want to tell, for each element of any of the lists in al4, whether it occurs in al2. If so, then you want

ArrayList<String> al3 = new ArrayList<String>();
Set<String> al2set = new HashSet<String>(al2);
for (List<String> tempList : al4)
    for (String temp : tempList)
        al3.add(al2set.contains(temp) ? "Yes" : "No");

This will give you one flat ArrayList<String>, recording, for each element of the lists of al4, whether that element was in al2 somewhere.

It is possible that you want your result to be an ArrayList<ArrayList<String>>, so that the result mimics the structure of al4, in which case it's only slightly more complicated:

ArrayList<ArrayList<String>> al3list = new ArrayList<ArrayList<String>>();
Set<String> al2set = new HashSet<String>(al2);
for (List<String> tempList : al4) {
    ArrayList<String> al3 = new ArrayList<String>();
    for (String temp : tempList)
        al3.add(al2set.contains(temp) ? "Yes" : "No");
    al3list.add(al3);
}

After executing this, a3list will have the same structure as al4, and each element of each list will be either "Yes" or "No", according to whether the corresponding element in al4 was contained somewhere in al2.

Sign up to request clarification or add additional context in comments.

5 Comments

Excellent and thorough explanation. Thanks ever so much, I really do appreciate it!
In this example, when I add another ArrayList to al4, on Execution it seems to add a duplication of the previous index to the next index, along with the correct values that should be stored in that index.
@user2988501 I'm not sure I follow what you mean. The code I gave will process the whole of al4. Are you wanting to add a new list to al4 and then just process that last bit? You might need to give an example if I've misunderstood what you're trying to do.
Yes, how would I edit the for loop, so that if I added another 'ArrayList' to the 'al4' it wouldn't cause duplicate values in 'al3list'.
I figured it out, I had a bracket in the wrong place!
1

You can create a method like this.

public static boolean[] compare(ArrayList<String> al1, ArrayList<String> al2) {

        boolean result[] = new boolean[al1.size()]; // result

        int i = 0;  // index

        /*
         * Test if an element of al1 occurs in al2
         */
        for(String s : al1) {
            if(al2.contains(s)) {
                result[i] = true;
            }
            else {
                result[i] = false;
            }
            i++;    // increment
        }

        return result; 
    }

And after you can call this method and display the result

boolean result[] = compare(al1, al2);

for(int i = 0; i < result.length; i++)  {
    System.out.print(result[i] + " ");
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.