在老师要求我们给我做这份作业的情况下
//Create a hash table where the initial storage
//is 7 and string keys can be mapped to Q values
我的问题是将字符串映射到Q值是什么意思?很抱歉,如果这是一个简单的问题,但这对我来说很新。
此外,我不确定是否会更改答案,但是在代码中我们不能使用任何Java Collections库,因此我必须从头开始对此进行编码
答案 0 :(得分:4)
因此,首先创建具有初始存储7的HashMap
,在下面的行中创建具有初始容量7的HashMap
,该容量接受String
类型的密钥和String
类型的值
HashMap<String, String> map = new HashMap<String, String>(7);
将键值添加到HashMap
map.put("hello", "world");
根据您需要使用HashMap
类型的键和String
类型的值创建Q
,所以我认为Q
必须是类或接口
HashMap<String, Q> map = new HashMap<String, Q>(7);
注意:哈希图将覆盖重复键的值
如果您不想为此使用Collections,则应创建具有实现的CustomHashMap
class HashMapCustom<K, V> {
private Entry<K,V>[] table; //Array of Entry.
private int capacity= 7; //Initial capacity of HashMap
static class Entry<K, V> {
K key;
V value;
Entry<K,V> next;
public Entry(K key, V value, Entry<K,V> next){
this.key = key;
this.value = value;
this.next = next;
}
}
public HashMapCustom(){
table = new Entry[capacity];
}
在上面的代码中,默认初始容量为7 HashMapCustom<String, Q> hashMapCustom = new HashMapCustom<String, Q>();