There is a class SingleWord with fields String word , String alphabet , int icon . There is an ArrayList<SingleWord> collection that stores SingleWord objects. How to check in the loop whether the word variable of each collection object contains the word str ?

    2 answers 2

    If you need to check whether the word each element of the list contains the substring "str" , then you can:

     boolean contains = true; for (SingleWord singleWord : words) { if (!singleWord.word.contains("str")) { contains = false; break; } } 

    When using the Android SDK 24+:

     boolean contains = words.stream().map(e -> e.word).allMatch(w -> w.contains("str")); 
    • It should be pointed out that this method will only work for applications with minSdk> 23 - temq
    • @temq did not notice the tag "android" in the question. Then the answer is generally better to delete. And what - with the SDK 24 in Android support for Java 8? - Regent
    • one
      Partial support has appeared, including streaming - temq
     List <SingleWorld> matches = new ArraList <>(); for(SingleWord word : words //твой лист){ if(word.getWord().contains(str) matches.add(word) //изменять коллекцию, которая перебирается в цикле нельзя } //здесь делай всё что хочешь с этими данными