Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - contains check all items in an arraylist meet a condition

Tags:

java

arraylist

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

like image 792
Prabu Avatar asked Aug 31 '15 06:08

Prabu


People also ask

How do you check if an ArrayList contains a value?

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.

How do you check if an ArrayList of objects contains a value in Java?

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.

How do you check if a list contains all elements of another list in Java?

The containsAll() method of List interface in Java is used to check if this List contains all of the elements in the specified Collection.

What does .contains do in ArrayList?

The contains() method checks if the specified element is present in the arraylist.


2 Answers

In Java8, you can use stream with matching to simplify your code.

 return arrayList.stream().allMatch(t -> t.toLowerCase().contains("test"));
like image 191
chengpohi Avatar answered Oct 07 '22 13:10

chengpohi


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;
}
like image 26
Suresh Atta Avatar answered Oct 07 '22 13:10

Suresh Atta