英文:
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<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);
}
</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);
}
}
注意:
- 我不确定你是想在
duckCreekToEDMStatusMapper
中检查duckStatus
还是在STATUS_MAP
中检查,我在这里使用了STATUS_MAP
。 - 在这种情况下,最好使用
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<String, String> STATUS_MAP = new HashMap<>();
static {
// Mapping the status values in Duck Creek to the Values used here
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("Invalid code " + duckStatus + " does not map to status code");
}
return STATUS_MAP.get(duckStatus);
}
}
Note:
- Not sure if you wanted to check for
duckStatus
in
duckCreekToEDMStatusMapper
orSTATUS_MAP
, I am usingSTATUS_MAP
- Instead of
NullPointerException
better to use
IllegalArgumentException
as it is appropriate here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论