myArrayList = {"Method and apparatus","system and method for the same","drive-modulation method"," METHOD FOR ORTHOGONAL"}
How can i check if all the Items (myArrayList) contains a word "method" (irrespective of case)
boolean method return true
if all the items contain the word, else its false
ArrayList. contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.
ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.
The containsAll() method of List interface in Java is used to check if this List contains all of the elements in the specified Collection.
The contains() method checks if the specified element is present in the arraylist.
In Java8, you can use stream with matching to simplify your code.
return arrayList.stream().allMatch(t -> t.toLowerCase().contains("test"));
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With