Java 字典项数组的数据结构
我正在寻找一种java原生方法(最好是)来实现一个数据结构,将int作为键,将一组键/值对作为值。本质上,if是一个索引引用的词汇表数组 例:Java 字典项数组的数据结构,java,Java,我正在寻找一种java原生方法(最好是)来实现一个数据结构,将int作为键,将一组键/值对作为值。本质上,if是一个索引引用的词汇表数组 例: println(foo[0].getValue(“hello”)将打印出“world”,println(foo[0].getValue(“so”)将打印出“rocks”,这里看一下hashmap 我想这就是你要找的地图怎么样 Map<Integer, Map<Key, Value>> myMap; Map-myMap; 一个
println(foo[0].getValue(“hello”)
将打印出“world”
,println(foo[0].getValue(“so”)
将打印出“rocks”
,这里看一下hashmap
我想这就是你要找的地图怎么样
Map<Integer, Map<Key, Value>> myMap;
Map-myMap;
一个具体的实现是
HashMap
映射接口就是您要寻找的,它似乎是一个特定的实现:
Map<Integer, Map<String, String>> map = new HashMap<>();
Map<String, String> someInsert = new HashMap<>();
someInsert.put("No", "Means no");
map.put(0, someInsert);
System.out.println(map.get(0).get("No"));
- 如果您事先知道字典的数量,则最小结构是映射数组:
Map<Key,Value>[] dictonaires = new HashMap<Key,Value>[20]; for (int i=0; i<dictionaries.length; i++) { dictionaries[i] = new Hashmap<Key,Value>(); } // Any time later, refer to a dictionary by index Map<Key,Value> currentDictionary = dictionaries[10]; // Can call currentDictionar.put/get/remove to create or update/read/delete // entries, but can't add/remove entire dictionaries
Means no
Map<Key,Value>[] dictonaires = new HashMap<Key,Value>[20];
for (int i=0; i<dictionaries.length; i++) {
dictionaries[i] = new Hashmap<Key,Value>();
}
// Any time later, refer to a dictionary by index
Map<Key,Value> currentDictionary = dictionaries[10];
// Can call currentDictionar.put/get/remove to create or update/read/delete
// entries, but can't add/remove entire dictionaries
List<Map<Key,Value>> dictionaryList = new ArrayList<Map<Key,Value>>();
// Then add new dictionary anytime later:
dictionaryList.add(new HashMap<Key,Value>());
// Access by index (index matches order of adding):
Map<Key,Value> currentDictionary = dictionaryList.get(10);
// Can call currentDictionar.put/get/remove to create or update/read/delete
// entries, but can't add/remove entire dictionaries
// Or even remove entire dictionary by index:
dictionaryList.remove(10);