我想知道如何使用 Java 实例化 Map < String, TreeMap < Integer, Integer >>。
谢谢!
您需要使用Map
接口的实现类之一,例如HashMap
或TreeMap
如下:
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);