How to Check if an ArrayList Contain 2 values?

前端 未结 6 1338
忘掉有多难
忘掉有多难 2021-01-19 09:34

is there any way to check if a collection contain a value or more with better performance than looping twice with contains?

in other meaning something that would loo

6条回答
  •  情深已故
    2021-01-19 10:09

    contains takes only one param

    if(person.contains("Joe") || person.contains("Jasha")){
      // do something
      }
    

    If you are worrying about N number of elements

    String[] items ={"Joe","jon","And So On".....};
    for (String item : items ) {
        if (person.contains(item)) {
           //found
            break; 
        } 
    }
    

    Edit:

    That || is most helpful in your case, since you have the benefit of Short-circuit_evaluation

    The second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression:

提交回复
热议问题