在java中跟随if-else梯的更好的替代方法是什么? [英] what is the better alternative to following if-else ladder in java?

查看:121
本文介绍了在java中跟随if-else梯的更好的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我正在检查文件名,文件名存储在名为 str String 变量中$ c>并根据中检查的条件if 语句我设置一个名为 mailType 的变量的值。

Situation : I am examining filenames , the filename is stored in a String variable called str and according to the conditions checked in if statements i am setting the value of a variable called 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中缩短我的代码并且内存友好吗?

Problem : Is there any better way to do this in java that shortens my code and is memory friendly ?

推荐答案

使用 LinkedHashMap< ; String,String>

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

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

Then iterate the map until you find a matching key:

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

这里的关键点是 LinkedHashMap 保留插入顺序(与 HashMap <不同/ code>)所以你可以实际指定你想要测试匹配的顺序(其他地图实现也这样做,例如Guava的 ImmutableMap ; LinkedHashMap 只是你开箱即用的。)

The key point here is that LinkedHashMap preserves insertion order (unlike HashMap) so you can actually specify the order in which you want to test for matches (other map implementations do this too, e.g. Guava's ImmutableMap; LinkedHashMap is simply one that you have out of the box).

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

If you need to nest this for the outer cases, you can simply apply the same pattern:

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

然后只需以相同的方式遍历密钥:

And then just iterate through the keys in the same way:

for (Map.Entry<String, LinkedHashMap<String, String>> outerEntry : outerMapping.entrySet()) {
  if (str.contains(outerEntry.getKey()) {
    // Apply the iteration above, using outerEntry.getValue().
  }
}

这篇关于在java中跟随if-else梯的更好的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆