Hashmap的key可不可以为null,map呢?
对于HashMap来说,可以存放null键和null值,而HashTable则不可以。
在hashmap中
public static void main(String[] args) {
Map map = new HashMap();
map.put(null,1);
System.out.println(map.get(null));
}
// 1
hashmap的get方法
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
hash()方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
这里直接判断了ket == null? 等于的话直接为零,这里相当于找了key = 0 的value。
Map
对于map来说,能不能存放null键和null值要看具体实现的方法。
对于HashTable:
public static void main(String[] args) {
Map map = new Hashtable();
map.put(null,1);
System.out.println(map.get(null));
}
Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:465)
at dailyExercise.test.thread.Rectangle.main(Rectangle.java:16)
Hashtable.put() 方法
public synchronized V put(K key, V value) {
// Make sure the value is not null
// 限制了 value 也不能为null ,如果value 为null,则npe
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
// key 不能为null
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
其他map集合类
对于Map里面的键和值是否可以为空的问题,答案是:不一定,具体要看map实现的是哪个类的方法。