所以首先创建HashMap,初始存储为7,下面的行创建HashMap,初始容量为7,接受String类型键和String类型值
HashMap<String, String> map = new HashMap<String, String>(7);
将键值添加到HashMap的示例
map.put("hello", "world");
根据你需要用String类型键和Q类型值创建HashMap,所以我相信Q必须是类或接口
HashMap<String, Q> map = new HashMap<String, Q>(7);
注意:hashmap 会覆盖重复键的值
如果您不想为此使用集合,那么您应该创建 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];
}
上述代码中默认初始容量为7HashMapCustom<String, Q> hashMapCustom = new HashMapCustom<String, Q>();
但是您仍然需要为put、delete、get 和您需要的方法编写自己的逻辑。我建议你检查这个ref1,ref2