繁体   English   中英

在java中跟随if-else梯的更好的替代方法是什么?

[英]what is the better alternative to following if-else ladder in java?

情况:我正在检查文件名,文件名存储在名为strString变量中,并根据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<String, String>

LinkedHashMap<String, String> mapping = new LinkedHashMap<>();
mapping.put("unsupported", "unsupported");
mapping.put("final_result", "final_result");
// ... etc

然后迭代地图,直到找到匹配的键:

for (Map.Entry<String, String> entry : mapping.entrySet()) {
  if (str.contains(entry.getKey()) {
    mailType = entry.getValue();
    break;
  }
}

这里的关键点是LinkedHashMap保留了插入顺序(与HashMap不同),因此你可以实际指定你想要测试匹配的顺序(其他地图实现也这样做,例如Guava的ImmutableMap ; LinkedHashMap只是你的一个盒子)。

如果你需要为外壳嵌套这个,你可以简单地应用相同的模式:

LinkedHashMap<String, LinkedHashMap<String, String>> outerMapping =
    new LinkedHashMap<>();
outerMapping.put("template", mapping);
outerMapping.put("properties", someOtherMapping);

然后以相同的方式迭代键:

for (Map.Entry<String, LinkedHashMap<String, String>> 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<String> 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM