Java 如何检查 ArrayList 是否包含 2 个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20755432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:06:25  来源:igfitidea点击:

How to Check if an ArrayList Contain 2 values?

javacollectionsarraylist

提问by Yehia Awad

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 look like this

换句话说,看起来像这样的东西

person.contains("Joe" || "Jasha");

采纳答案by Bohemian

The implementation of ArrayList.contains()loops through every element and does an equals()test, so calling .contains()twice is inefficient.

实现ArrayList.contains()遍历每个元素并进行equals()测试,因此调用.contains()两次效率低下

You could write your own loop that check both at once using a compiled regex Patternthat looks for either name at the same time:

您可以编写自己的循环,使用同时查找任一名称的已编译正则表达式模式一次检查两者:

Pattern p = Pattern.compile("Joe|Jasha");
boolean found = false;
for (String s : person) {
    if (p.matcher(s).find()) {
        found = true;
        break;
    }
}

回答by Suresh Atta

contains takes only one param

contains 只需要一个参数

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

If you are worrying about Nnumber of elements

如果您担心N元素的数量

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

||对您的情况最有帮助,因为您受益于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:

仅当第一个参数不足以确定表达式的值时,才执行或评估第二个参数:

回答by Shiju K Babu

You have to search each string.

您必须搜索每个字符串。

UPDATED

更新

List list=new ArrayList();
list.add("Joe");
list.add("Jasha");
if(person.containsAll(list))
{
   //do something
}

回答by Infinite Recursion

You can also use regex as follows:

您还可以按如下方式使用正则表达式:

person.toString().matches(".*\b(Joe|Jasha)\b.*");

You will get a boolean value indicating whether it is present or not.

您将获得一个布尔值,指示它是否存在。

回答by Chandrayya G K

Simply use Arrays class

只需使用 Arrays 类

person.containsAll(Arrays.asList("Jasha","Joe"));

回答by awwsmm

You can also do

你也可以这样做

String person1 = "Joe Smith"; // contains "Joe"
String person2 = "Jasha Thompson"; // contains "Jasha"
String person3 = "Jasha Joesephson"; // contains "Joe" and "Jasha"
String person4 = "Steve Smith"; // contains neither

public boolean containsJoeOrJasha(String stringToCheck) {
  return !Collections.disjoint(
    Arrays.asList(stringToCheck.split(" ")),
    Arrays.asList("Joe", "Jasha"));
}

Collections.disjoint returns true if the two collections are disjoint, meaning that they have no elements in common. So...

如果两个集合不相交,则 Collections.disjoint 返回 true ,这意味着它们没有共同的元素。所以...

jshell> containsJoeOrJasha(person1)
 ==> true

jshell> containsJoeOrJasha(person2)
 ==> true

jshell> containsJoeOrJasha(person3)
 ==> true

jshell> containsJoeOrJasha(person4)
 ==> false