如何从HashMap中获取值

时间:2014-09-16 06:36:03

标签: java hashmap

我有一个HashMap HashMap<String, String> cntrlInfo = new HashMap<String, String>(); 在这个地图中有一些值,我需要逐个读取值,我希望将值存储在字符串中。 例如{GBP,001,101,CHDP} 现在我想存储 String ccy=GBP;我如何分配字符串

中的值

1 个答案:

答案 0 :(得分:0)

import java.util.HashMap;

public class Test {

    public static void main(String[] args)  {
        HashMap<String, String> cntrlInfo = new HashMap<String, String>();
        cntrlInfo.put("A", "GBP");
        cntrlInfo.put("B", "001");
        cntrlInfo.put("C", "101");
        cntrlInfo.put("D", "CHDP");
        StringBuilder sb = new StringBuilder();
        for (String val : cntrlInfo.values()) {
            sb.append(val);
        }

        System.out.println("sb = " + sb);
        String ccy = sb.substring(sb.indexOf("GBP"), sb.indexOf("GBP") + 3);
        String foo = sb.substring(sb.indexOf("001"), sb.indexOf("001") + 3);
        String bar = sb.substring(sb.indexOf("101"), sb.indexOf("101") + 3);
        String f = sb.substring(sb.indexOf("CHDP"), sb.indexOf("CHDP") + 4);

        System.out.println("ccy = " + ccy);
        System.out.println("foo = " + foo);
        System.out.println("bar = " + bar);
        System.out.println("f = " + f);
    }
}