expected output for list.contains("sales"); //true
This is cannot be done with standard JDK implementation of List
/ArrayList
.
Similarly, the input list needs to be iterated one way or another to check if any of its String entries contains the given substring, whether each entry is checked separately (in a loop or a stream) or whether all the entries are joined in a bigger string (using List::toString
or String::join/ String::valueOf
, etc.)
Assuming that the implicit iteration is not counted as "iteration", one of the possible solutions would be just to check if the list string representation contains the string:
list.toString().toLowerCase().contains("sales") // -> true
Another solution is to use String::matches
(however, the search string needs to be surrounded with ".*"
when building the regex pattern):
list.toString().matches("(?i).*sales.*"); // -> true