我有一个方法可以从数据库中读取并从中获取一些字符串。根据我得到的,我将覆盖另一个我已经知道的字符串。例如:
str
→string
bin
→binary
- 等等..
我的问题是,这样做的最佳做法是什么?当然我已经考虑过如果...
if (str.equals("str"))
str = "string";
预定义了这些东西的文件,多维数组等。但这一切似乎都是新手,所以你推荐什么?什么是最好的方法?
最佳答案
使用 map :
// create a map that maps abbreviated strings to their replacement text
Map<String, String> abbreviationMap = new HashMap<String, String>();
// populate the map with some values
abbreviationMap.put("str", "string");
abbreviationMap.put("bin", "binary");
abbreviationMap.put("txt", "text");
// get a string from the database and replace it with the value from the map
String fromDB = // get string from database
String fullText = abbreviationMap.get(fromDB);
关于java - java中方法的良好实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15622365/