Refactor this java code to check whether the string contains any of these possibilities?
•
Java
I need to check the possibility that a string does not contain any of these strings:
MNC bra LEB Mar RVC method GLZ World Wide Web hyb
My current code:
if(selectedLocation.equals("OTH")) { if(!currentClassLocation.equals("MNC") && !currentClassLocation.equals("BRA") && !currentClassLocation.equals("LEB") && !currentClassLocation.equals("MAR") && !currentClassLocation.equals("RVC") && !currentClassLocation.equals("WAY") && !currentClassLocation.equals("GLZ") && !currentClassLocation.equals("WWW") && !currentClassLocation.equals("HYB")) { //Awesome,I need this string! I operate on it here. } }
To make a long story short, I can't use for loop Is there any way I can check if the string contains any of these iterations?
Solution
Use HashSet:
Set<String> invalidSequences = new HashSet<String>(); invalidSequences.add("MNC"); invalidSequences.add("BRA"); invalidSequences.add("LEB"); // Remaining sequences ... if (!invalidSequences.contains(currentClassLocation)) { // Awesome,I need this string... }
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
二维码