Skip to main content
added 6 characters in body; edited tags; edited title
Source Link
cнŝdk
  • 32.2k
  • 7
  • 62
  • 81

check if an ArrayList contains all Strings from another ArrayList

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)firstArray(true) than that second(true)second(true) and third(false)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); } } 

ArrayList contains all Strings from another ArrayList

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); } } 

check if an ArrayList contains all Strings from another ArrayList

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); } } 
added 59 characters in body
Source Link

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); } } 

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)

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); } } 

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); } } 
deleted 101 characters in body
Source Link
willeM_ Van Onsem
  • 481.8k
  • 33
  • 483
  • 624

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)

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);   }  } 

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)

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);   }  } 

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)

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); } } 
Source Link
Loading