java - java中方法的良好实践

标签 java

我有一个方法可以从数据库中读取并从中获取一些字符串。根据我得到的,我将覆盖另一个我已经知道的字符串。例如:

  • strstring
  • binbinary
  • 等等..

我的问题是,这样做的最佳做法是什么?当然我已经考虑过如果...

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);

您可以 read more about Maps here .

关于java - java中方法的良好实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15622365/

相关文章:

java 如何获取数组中的红色值

java - 关于 java serverSocket 的奇怪响应

java - 使参数在应用程序中可用

java - 自定义异常 : Differentiate via many subclasses or single class backed with enum?

java - GWT 和 CSS 开发最佳实践

java - 使用 LWJGL 时是否需要使用 glDelete*

java - 在 Eclipse 中自动为类创建构建器

java - 考虑到内存限制,是否有另一种方法可以在 Java 中处理大型二维数组?

java - 如何使用 MapStruct 仅映射指定字段?

java - 全局状态变量的静态与实例