C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Return: The isMoth method returns true if a moth name is detected, and false otherwise.
ReturnDefault: When no string is matched in the isMoth method, we reach the default case, where false is returned.
Java program that uses string switch
public class Program {
static boolean isMoth(String value) {
// Switch on a string.
// ... Match names of some moths.
switch (value) {
case "Atlas Moth":
case "Beet Armyworm":
case "Indian Meal Moth":
case "Ash Pug":
case "Latticed Heath":
case "Ribald Wave":
case "The Streak":
return true;
default:
return false;
}
}
public static void main(String[] args) {
// Test the isMoth method.
System.out.println(isMoth("Atlas Moth"));
System.out.println(isMoth("atlas moth")); // Case matters.
System.out.println(isMoth("Chiloe"));
}
}
Output
true
false
false
IsMoth: This uses a switch statement to detect the names of moths. It is the same as the first example.
IsMothOr: This uses an expression with string equality and logical ors. It could be written as an if-statement.
IfResult: After 5 runs of each method, I found that the expression-based isMothOr performed better.
Warning: This benchmark has flaws, but isMothOr seems better. Benchmarks are optimized by the JIT compiler, so they can be misleading.
Java program that benchmarks string switch, expression
public class Program {
static boolean isMoth(String value) {
switch (value) {
case "Atlas Moth":
case "Beet Armyworm":
case "Indian Meal Moth":
case "Ash Pug":
case "Latticed Heath":
case "Ribald Wave":
case "The Streak":
return true;
default:
return false;
}
}
static boolean isMothOr(String value) {
return value == "Atlas Moth" || value == "Beet Armyworm"
|| value == "Indian Meal Moth" || value == "Ash Pug"
|| value == "Latticed Heath" || value == "Ribald Wave"
|| value == "The Streak";
}
public static void main(String[] args) throws Exception {
String[] tests = { "Ribald Wave", "Java", "Beet Armyworm", "Python" };
// Call both methods.
boolean x = isMoth("");
x = isMothOr("");
System.out.println(x);
long t1 = System.currentTimeMillis();
// ... Change isMoth to isMothOr to test expression version.
for (int i = 0; i < 100000000; i++) {
int count = 0;
for (String test : tests) {
if (isMoth(test)) {
count++;
}
}
if (count != 2) {
throw new Exception();
}
}
long t2 = System.currentTimeMillis();
// ... Time.
System.out.println(t2 - t1);
}
}
Output
isMoth: 1058 ms
isMoth: 1672 ms
isMoth: 1672 ms
isMoth: 1672 ms
isMoth: 1781 ms
isMothOr: 844 ms
isMothOr: 859 ms
isMothOr: 843 ms
isMothOr: 688 ms
isMothOr: 875 ms
ContainsKey: I use containsKey on the HashMap to search for a matching string value. This computes a hash code.
Result: On the test system, I found that the HashMap was faster than a string switch, but slower than the expression version.
Java program that benchmarks HashMap
import java.util.HashMap;
public class Program {
public static void main(String[] args) throws Exception {
String[] tests = { "Ribald Wave", "Java", "Beet Armyworm", "Python" };
// Initialize HashMap.
HashMap<String, Boolean> hash = new HashMap<>();
hash.put("Atlas Moth", true);
hash.put("Beet Armyworm", true);
hash.put("Indian Meal Moth", true);
hash.put("Ash Pug", true);
hash.put("Latticed Heath", true);
hash.put("Ribald Wave", true);
hash.put("The Streak", true);
long t1 = System.currentTimeMillis();
// ... This version uses containsKey on a HashMap.
for (int i = 0; i < 100000000; i++) {
int count = 0;
for (String test : tests) {
if (hash.containsKey(test)) {
count++;
}
}
if (count != 2) {
throw new Exception();
}
}
long t2 = System.currentTimeMillis();
// ... Time.
System.out.println(t2 - t1);
}
}
Output
containsKey: 1108 ms
containsKey: 1119 ms
containsKey: 1117 ms
containsKey: 1126 ms
containsKey: 1108 ms
Switch: The string switch is convenient, but in my test it was slower than either the expressions or the HashMap.