在可重用的Java方法中填充常量映射的最佳做法

huangapple go评论66阅读模式
英文:

Best practice for populating constant map used in reusable method Java

问题

我正在使用可重用的方法来映射两个代码,当我们接收到'a'时,我们希望在我们的代码中使用'01'。这是一个静态方法,所以它只会被创建一次,然后我们重复使用这个方法,这样做是正确的吗?还是这种做法有问题,每次调用该方法时都会不必要地进行处理?这是良好的做法,还是我应该在其他地方创建这个映射,然后只使用方法来返回其中的值?

private static String mapStatus(String duckStatus){

   
    Map<String, String> statusMap = new HashMap<String,String>();
    statusMap.put("a","01");
    statusMap.put("b","02");
    statusMap.put("c","03");

    if (!statusMap.containsKey(duckStatus)){
        throw new NullPointerException("Invalid Code does not map to status code");
    }
    return statusMap.get(duckStatus);
}
英文:

I am using a reusable method to map two codes, when we receive 'a' we want to use '01' in our code. This is a static method so is it right that it only gets created once and we reuse the method or is this bad practice and it is unnecessarily processing every time we call this method? this good practice or should I be creating this map somewhere else and just using the method for returning the values from it?

    private static String mapStatus(String duckStatus){

       
        Map&lt;String, String&gt; statusMap = new HashMap&lt;String,String&gt;();
        statusMap.put(&quot;a&quot;,&quot;01&quot;);
        statusMap.put(&quot;b&quot;,&quot;02&quot;);
        statusMap.put(&quot;c&quot;,&quot;03&quot;);

        if (!statusMap.containsKey(duckStatus)){
            throw new NullPointerException(&quot;Invalid Code does not map to status code&quot;);
        }
        return statusMap.get(duckStatus);
    }
    

</details>


# 答案1
**得分**: 2

根据评论,`enum` 可能是一个不错的选择,但正如 @kendavidson 已经提到的,这是有争议的。从性能的角度来看,我在这里并没有看到 `enum` 和 `HashMap` 之间有太大的区别,但在我看来,`HashMap` 更合适,并且会提供更好的可读性。我建议在你已经编写的代码中只做一个不同的修改,将 `HashMap` 静态填充,以避免每次调用 `mapStatus` 方法时都要构建 `HashMap`。修改后的代码如下:

```java
public class Test {
    private static final Map<String, String> STATUS_MAP = new HashMap<>();
    static {
        // 将 Duck Creek 中的状态值映射到此处使用的值
        STATUS_MAP.put("a", "01");
        STATUS_MAP.put("b", "02");
        STATUS_MAP.put("c", "03");
    }

    private static String mapStatus(String duckStatus) {
        if (!STATUS_MAP.containsKey(duckStatus)) {
            throw new IllegalArgumentException("无效的代码 " + duckStatus + " 无法映射到状态代码");
        }
        return STATUS_MAP.get(duckStatus);
    }
}

注意:

  1. 我不确定你是想在 duckCreekToEDMStatusMapper 中检查 duckStatus 还是在 STATUS_MAP 中检查,我在这里使用了 STATUS_MAP
  2. 在这种情况下,最好使用 IllegalArgumentException 而不是 NullPointerException
英文:

As per the comments, enum can be a good choice but again as already mentioned by @kendavidson it is debatable. From the performance angle I don't see much of a difference between enum and HashMap here but in my opinion HashMap will be more appropriate and will provide better readability. Only difference I suggest in the code you have written is, make the HashMap statically populated so that HashMap build up time is avoided every time you want to lookup the statusMap whenever the call is made to mapStatus method. The modified code will look like below:

public class Test {
    private static final Map&lt;String, String&gt; STATUS_MAP = new HashMap&lt;&gt;();
    static {
        // Mapping the status values in Duck Creek to the Values used here
        STATUS_MAP.put(&quot;a&quot;, &quot;01&quot;);
        STATUS_MAP.put(&quot;b&quot;, &quot;02&quot;);
        STATUS_MAP.put(&quot;c&quot;, &quot;03&quot;);
    }

    private static String mapStatus(String duckStatus) {
        if (!STATUS_MAP.containsKey(duckStatus)) {
            throw new IllegalArgumentException(&quot;Invalid code &quot; + duckStatus + &quot; does not map to status code&quot;);
        }
        return STATUS_MAP.get(duckStatus);
    }
}

Note:

  1. Not sure if you wanted to check for duckStatus in
    duckCreekToEDMStatusMapper or STATUS_MAP, I am using STATUS_MAP
  2. Instead of NullPointerException better to use
    IllegalArgumentException as it is appropriate here.

huangapple
  • 本文由 发表于 2020年8月28日 23:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63636624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定