13
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

Prabu
  • 3,550
  • 9
  • 44
  • 85

6 Answers6

49

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

 return arrayList.stream().allMatch(t -> t.toLowerCase().contains("test"));
gMale
  • 17,147
  • 17
  • 91
  • 116
chengpohi
  • 14,064
  • 1
  • 24
  • 42
  • 2
    If you're using Java 8 please use the right methods. OP wants to check if every item contains the String. Therefore use `allMatch` instead of filter – Flown Aug 31 '15 at 07:03
11

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;
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2
    public boolean listContainsAll(List<String> list) {
    for (String item : list) {
        if (!item.toLowerCase().contains("method")) {
            return false;
        }
    }

    return true;
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Kristian Vukusic
  • 3,284
  • 6
  • 30
  • 46
2

Simple loop checking condition, added white chars for avoiding wrong words as 'somewordmethod':

    boolean result = true;
    for (String elem : yourList) {
        if (!elem.toLowerCase().contains(" method ")) {
            result = false;
            break;
        }
    }
    return result;


 return result;
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Fran Montero
  • 1,679
  • 12
  • 24
2

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;
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
2

ArrayList implements the List Interface.

If you look at the Javadoc for List at the contains method you will see that it uses the equals() method to evaluate if two objects are the same.

int tempCount = 0;
for (String str : arraylist) {
    if(str.conatains("method") || str.conatains("Method")) {
        tempCount++;
    }    
}

if(tempCount == arraylist.size()) {
    return true;
} else {
    return false;
}
technomage
  • 9,861
  • 2
  • 26
  • 40
SaviNuclear
  • 886
  • 1
  • 7
  • 19
  • 2
    This answer is copy-pasted from http://stackoverflow.com/a/2642709. Savi, you can't just copy other people's answers; at the very least you *have to* give a link back to the answer you copied. – JJJ Aug 31 '15 at 07:30
  • What does equals() have to do with the price of beans? – NomadMaker May 17 '20 at 15:54