使用预定义值填充哈希图(java)

Populating a hashmap with predefined values (java)(使用预定义值填充哈希图(java))

本文介绍了使用预定义值填充哈希图(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个以前没有处理过的问题.我正在为java中的数据库编写一个补丁,它基本上转换存储在某些行中的数据.为了做到这一点,我有一个转换表,可以告诉我什么值变成什么值.

I've run into a problem I haven't had to deal with before. I'm writing a patch for a database in java that's basically converting data stored in certain rows. In order to do this I have a conversion table that tells me what values become what.

例如,如果我读入RC"、AC"、GH"-> 将值更新为T1".(这些只是随机示例,它基本上是将一个字符串转换为另一个字符串.)

Example, if I read in either "RC", "AC", "GH" -> Update the value to "T1". (These are just random examples, it's basically converting one string to another.)

我需要一种存储这些转换的好方法.我在想一个哈希图:KEY,VALUE: (RC,T1) (AC,T1) (GH,T1) 以此类推.

I need a good way of storing these conversions. I was thinking a hashmap: KEY,VALUE: (RC,T1) (AC,T1) (GH,T1) and so on and so on.

现在,有几十个.补丁初始化时填充此哈希图的好方法是什么?

Now, there's dozens and dozens of these. What's a good clean way of populating this hashmap when the patch initializes?

推荐答案

我会在设置 HashMap 时进行初始化

I would do the initialisation while setting up the HashMap

例如

private static final Map<String, String> m = new HashMap<String, String>() {{
    put("RC", "T1");
    put("AC", "T1");
}};

然后,您需要确保所有内容都在您的代码中一起设置.

Then you wuld make sure that everything is set up together in your code.

我认为@Nambari 提出了一个很好的观点,尽管它可能将值作为一个列表而不仅仅是一个字符串.不过,这确实会交换您的键和值.

I think @Nambari makes a good point though with perhaps having the value as a list rather than just a string. This does then swap your keys and values though.

例如

 private static final Map<String, List<String>> m = new HashMap<String, List<String>>() {{
    put("T1", Arrays.asList("RC", "AC");
}};

这篇关于使用预定义值填充哈希图(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用预定义值填充哈希图(java)

基础教程推荐