How to check whether ArrayList contains 2 values?

Is there a way to check whether a collection contains one or more values and has better performance than using the contains loop twice?

Something that looks like this in other senses

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

Solution

ArrayList. The implementation of contains () loops through each element and performs the equals () test, so it is called twice Contains() is inefficient

You can write your own loop and use the compiled regular expression pattern to check two names 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;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>