How to check whether ArrayList contains 2 values?
                                        
                    •
                    Java                                    
                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
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        