0

I need to check if all Strings from ArrayList are present in another ArrayList. I can use containsAll but this is not what I want to achieve. Let's me show you this on example:

assertThat(firstArray).containsAll(secondArray);

This code will check if all items from one array is in another one. But I need to check that every single item from one array is contained in any place in the second array.

List<String> firstArray = new ArrayList<>;
List<String> secondArray = new ArrayList<>;
firstArray.add("Bari 1908")
firstArray.add("Sheffield United")
firstArray.add("Crystal Palace")

secondArray.add("Bari")
secondArray.add("Sheffield U")
secondArray.add("C Palace")

So I want to check if first item from secondArray is in firstArray(true) than that second(true) and third(false). I wrote the code which is doing this job but it's quite complicated and I would like to know if there is any simpler way to achieve this goal (maybe with using hamcrest matchers or something like that)

ArrayList<String> notMatchedTeam = new ArrayList<>();
for (int i = 0; i < secondArray.size(); i++) {
    String team = secondArray.get(i);
    boolean teamMatched = false;
    for (int j = 0; j < firstArray.size(); j++) {
        teamMatched = firstArray.get(j).contains(team);
        if (teamMatched) {
            break;
        }
    }
    if (!teamMatched) {
        notMatchedTeam.add(team);
    }
}
5
  • So you want to check that every string in secondArray is a substring of some string in firstArray? Commented Apr 13, 2017 at 10:20
  • If I understand, you want to see if every string in one list, is a substring in the other list. In your example teamMatched will be false as the last string is not a substring. Commented Apr 13, 2017 at 10:21
  • Java does not provide any such library method to achieve this. And it makes sense not to provide these type of methods. All you can do is probably use streams and restructure your code. Commented Apr 13, 2017 at 10:22
  • Yes, exactly as you wrote guys, I want to check if every string of 'secondArray' is a substring in 'firstArray' Commented Apr 13, 2017 at 10:59
  • @gaganbm Yes, it looks like streams and refactoring can be best way. At most, I leave code like it is right now. Commented Apr 13, 2017 at 11:27

3 Answers 3

1

You can do something like this

     List<String> firstArray = new ArrayList<>();
            List<String> secondArray = new ArrayList<>();
            firstArray.add("Bari 1908");
            firstArray.add("Sheffield United");
            firstArray.add("Crystal Palace");

            secondArray.add("Bari");
            secondArray.add("Sheffield U");
            secondArray.add("C Palace");

           Set<String> firstSet= firstArray
                    .stream()
                    .collect(Collectors.toSet());

         long count=  secondArray.stream().filter(x->firstSet.contains(x)).count();

///
  Map<String, Boolean> result =
                secondArray.stream().collect(Collectors.toMap(s->s, firstSet::contains));

If count >0, then there are some items in second array which are not there in first.

result contains the string with its status.

Thanks

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

2 Comments

Interesting but I need to write assertions and would be nice to get info why my test failed, which elements from secondArray aren't present in the firstArray and so on. With these solution I got only boolean value count > 0 or not. Test failed or passed.
Your code doesn't check if item from second array is substring in the first. It check if any item from second is in the first. Here is example of your code: ideone.com/h89P1r "Bari" as well as "Sheffield U" should be true.
0

If you have space concerns like you have millions of words in one file and need to check entry of second file in first then use trie. From first make trie and check every entry of second in first.

Comments

0

Situation:

In your question you said that you wanted to return for each element if it exists or not, and in your actual code you are only returning a list of matching elements.

Solution:

You need to return a list of Boolean results instead, this is the code you need:

public static List<Boolean> whichElementsFound(List<String> firstList, List<String> secondList){
        ArrayList<Boolean> resultList = new ArrayList<>();
        for (int i = 0; i < secondList.size(); i++) {
            String team = secondList.get(i);
            resultList.add(firstList.contains(team));
        }
        return resultList;
}

Demo:

This is a working Demo using this method, returning respectively a List<Boolean> to reflects which element from the first list are found in the second.

Edit:

If you want to return the list of elements that were not found, use the following code:

public static List<String> whichElementsAreNotFound(List<String> firstList, List<String> secondList){
    ArrayList<String> resultList = new ArrayList<>();
    for (int i = 0; i < secondList.size(); i++) {
        String team = secondList.get(i);
        if(!firstList.contains(team)){
            resultList.add(team);
        }
    }
    return resultList;
}

This is the Demo updated.

10 Comments

Here you comparing first item from firstArray with first item from the second and so on. You need to do double for loop to check if first item from secondArray is in any place in the firstArray.
@cbronson1 No actually each element is compared to all second list elemnts withcontains() method.
Hmmm, actually I don't want to get boolean ArrayList but ArrayList with values which doesn't match any value in firstArray. I rewrite your code to got that List and surprisingly result are the same as using assertThat(firstArray).containsAll(secondArray);
Earlier I rewrite the code the same way as you did now and result are not as expected. Please take a look for my demo: ideone.com/3mjezn This should work this way but I don't get it why your code doesn't act like this
Why are you using 2 loops? Do you want to test if strings from second list contains string from first list?!
|

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.