Java - contains check all items in an arraylist meet a condition
In Java8, you can use stream with matching to simplify your code.
return arrayList.stream().allMatch(t -> t.toLowerCase().contains("test"));
public boolean listContainsAll(List<String> list) {
for (String item : list) {
if (!item.toLowerCase().contains("method")) {
return false;
}
}
return true;
}
You will have to check for the whole arraylist and return false if there is a string without that word.
public static void main(String[] args) {
ArrayList<String> list = new ArrayList();
list.add("I have the name");
list.add("I dont have the number");
list.add("I have a car");
System.out.println(check(list, "I"));
}
private static boolean check(ArrayList<String> list, String word) {
// TODO Auto-generated method stub
for(String s : list)
if(!list.contains(word))
return false;
return true;
}
Iterate and use contains. Remove the or conditions if you want case specific.
public static boolean isListContainMethod(List<String> arraylist) {
for (String str : arraylist) {
if (!str.toLowerCase().contains("method")) {
return false;
}
}
return true;
}