将java地图转换为spring map配置

时间:2015-06-15 06:12:07

标签: java spring configuration

我在java程序中创建了一个如下图所示的地图,现在我想通过spring方式制作这个程序中使用的地图我希望通过spring xml配置它们,rite现在是程序的输出

TradeRef = AAA
TF = AAA
Traes = AAA
Deaswe = BBB
TraRef = AAA
Dealdt = BBB
Daelet = BBB
Deadery = BBB

所以你可以看到值现在在地图中是硬编码的,因为它在我试图在春天转换的java程序下面,特别是关于Map配置spring xml的方式所以请告诉我如何设置春天地图本身的价值如何,我的春天地图xml将获得相同的功能

如下所示是我想配置的键值对

KEY          VALUE 
TF             AAA
Traes          AAA
Deaswe         BBB
TraRef         AAA
Dealdt         BBB
Daelet         BBB
Deadery        BBB

下面是java类

public class testMap {

    public static void main(String[] args)
    {


        Map<String, List<String>> dataMap = new HashMap<String, List<String>>();
        dataMap.put ("AAA", Arrays.asList("TradeRef", "TraRef", "TF", "Traes"));
        dataMap.put ("BBB", Arrays.asList("Dealdt", "Daelet", "Deadery", "Deaswe"));


        Map<String, String> itemMap = new HashMap<String, String>(); //New map for item->key mapping
        for(String key: dataMap.keySet()) //Get all keys and iterate through
            for(String item: dataMap.get(key)) //For each item in your value list
                itemMap.put(item, key); //Create new mapping item->key



         Iterator it = itemMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                it.remove(); // avoids a ConcurrentModificationException





         }
    }
}

0 个答案:

没有答案