在java中跟随if-else梯的更好的替代方法是什么?
情况:我正在检查文件名,文件名存储在名为str
的String
变量中,并根据if
语句中检查的条件设置名为mailType
的变量的值。
if(str.contains("template")) { if(str.contains("unsupported")) mailType="unsupported"; else if(str.contains("final_result")) mailType="final_result"; else if(str.contains("process_success")) mailType="Process Success"; else if(str.contains("receive")) mailType="Receive"; else if(str.contains("sen")) mailType="sent"; else if(str.contains("welcome")) mailType="welcome"; else if(str.contains("manual")) mailType="Manual"; } else if(str.contains("properties")) { if(str.contains("unsupported")) mailType="unsupported"; else if(str.contains("final_result")) mailType="final_result"; else if(str.contains("process_success")) mailType="Process Success"; else if(str.contains("receive")) mailType="Receive"; else if(str.contains("sen")) mailType="sent"; else if(str.contains("welcome")) mailType="welcome"; else if(str.contains("manual")) mailType="Manual"; }
问题:有没有更好的方法在java中缩短我的代码并且内存友好?
使用LinkedHashMap
:
LinkedHashMap mapping = new LinkedHashMap<>(); mapping.put("unsupported", "unsupported"); mapping.put("final_result", "final_result"); // ... etc
然后迭代地图,直到找到匹配的键:
for (Map.Entry entry : mapping.entrySet()) { if (str.contains(entry.getKey()) { mailType = entry.getValue(); break; } }
这里的关键点是LinkedHashMap
保留了插入顺序(与HashMap
不同),因此你可以实际指定你想要测试匹配的顺序(其他地图实现也这样做,例如Guava的ImmutableMap
; LinkedHashMap
只是你的一个盒子)。
如果你需要为shell嵌套这个,你可以简单地应用相同的模式:
LinkedHashMap> outerMapping = new LinkedHashMap<>(); outerMapping.put("template", mapping); outerMapping.put("properties", someOtherMapping);
然后以相同的方式迭代键:
for (Map.Entry> outerEntry : outerMapping.entrySet()) { if (str.contains(outerEntry.getKey()) { // Apply the iteration above, using outerEntry.getValue(). } }
你说你发现安迪的回答 (这就是我要做的)太复杂了。 他的原始评论暗示else if
可能是你的选择:
if (str.contains("template")) { if (str.contains("unsupported")) mailType = "unsupported"; else if (str.contains("final_result")) mailType = "final_result"; else if (str.contains("process_success")) mailType = "Process Success"; else if (str.contains("receive")) mailType = "Receive"; else if (str.contains("sen")) mailType = "sent"; else if (str.contains("welcome")) mailType = "welcome"; else if (str.contains("manual")) mailType = "Manual"; } else if (str.contains("properties")) { if (str.contains("unsupported")) mailType = "unsupported"; else if (str.contains("final_result")) mailType = "final_result"; else if (str.contains("process_success")) mailType = "Process Success"; else if (str.contains("receive")) mailType = "Receive"; else if (str.contains("sen")) mailType = "sent"; else if (str.contains("welcome")) mailType = "welcome"; else if (str.contains("manual")) mailType = "Manual"; }
或者更好,一直使用{}:
if (str.contains("template")) { if (str.contains("unsupported")) { mailType = "unsupported"; } else if (str.contains("final_result")) { mailType = "final_result"; } else if (str.contains("process_success")) { mailType = "Process Success"; } else if (str.contains("receive")) { mailType = "Receive"; } else if (str.contains("sen")) { mailType = "sent"; } else if (str.contains("welcome")) { mailType = "welcome"; } else if (str.contains("manual")) { mailType = "Manual"; } } else if (str.contains("properties")) { if (str.contains("unsupported")) { mailType = "unsupported"; } else if (str.contains("final_result")) { mailType = "final_result"; } else if (str.contains("process_success")) { mailType = "Process Success"; } else if (str.contains("receive")) { mailType = "Receive"; } else if (str.contains("sen")) { mailType = "sent"; } else if (str.contains("welcome")) { mailType = "welcome"; } else if (str.contains("manual")) { mailType = "Manual"; } }
试试这个
static String containsAndValue(Collection collection, String oldValue, String... strings) { if (strings.length % 2 != 0) throw new IllegalArgumentException("strings"); for (int i = 0; i < strings.length; i += 2) if (collection.contains(strings[i])) return strings[i + 1]; return oldValue; }
和
if (str.contains("template")) { mailType = containsAndValue(str, mailType, "unsupported", "unsupported", "final_result", "final_result", "process_success", "Process Success", "receive", "Receive", "sen", "sent", "welcome", "welcome", "manual", "Manual"); } else if (str.contains("properties")) { mailType = containsAndValue(str, mailType, "unsupported", "unsupported", "final_result", "final_result", "process_success", "Process Success", "receive", "Receive", "sen", "sent", "welcome", "welcome", "manual", "Manual"); }
这不那么复杂吗?
public enum MailType { UNSUPPORTED("unsupported", "unsupported"), FINAL_RESULT("final_result", "final_result"), PROCESS_SUCCESS("process_success", "Process Success"), RECEIVE("receive", "Receive"), SENT("sen", "sent"), WELCOME("welcome", "welcome"), MANUAL("manual", "Manual"); private final String query; private final String value; MailType(String query, String value) { this.query = query; this.value = value; } public static String find(String text){ for(MailType mailType: values()){ if(text.contains(mailType.query)) { return mailType.value; } } return null; } }
并像这样使用它:
String mailType = MailType.find(str);
首先,如果你使用else if
而不是简单的else
,你将减少缩进。
if(str.contains("unsupported")) mailType="unsupported"; else if(str.contains("final_result")) mailType="final_result"; else if(str.contains("process_success")) mailType="Process Success"; else if(str.contains("receive")) mailType="Receive"; else if(str.contains("sen")) mailType="sent"; else if(str.contains("welcome")) mailType="welcome"; else if(str.contains("manual")) mailType="Manual";
在您的特定情况下,我建议尝试使用正则表达式来简化您的工作:
Pattern p = Pattern.compile("(unsupported|final_result|welcome)"); Matcher m = p.matcher(str); if (m.matches()) { mailType = m.group(1); }