如何实例化Map <string,treemap <integer,=“”integer =“”>&gt;?</string,>

时间:2012-11-13 06:26:02

标签: java map constructor

我想知道如何实例化Map&lt; String,TreeMap&lt;整数,整数&gt; &gt;,使用Java。

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要使用Map接口的其中一个实现类,例如HashMapTreeMap如下:

    Map < String, TreeMap < Integer, Integer > > myMap = 
                          new HashMap< String, TreeMap < Integer, Integer > >();

    Map < String, TreeMap < Integer, Integer > > myMap = 
                          new TreeMap< String, TreeMap < Integer, Integer > >();

添加值:

    //Instantiate TreeMap element as below  
    TreeMap< Integer, Integer > elemMap = new TreeMap<Integer, Integer>();

    //add elements to tree map
    elemMap.put(1, 100);

    //put the tree map in the map
    myMap.put("firstElem", elemMap);