publicsynchronized V put(K key, V value){ // Make sure the value is not null if (value == null) { thrownew NullPointerException(); }
// Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; 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; } }
// Exception in thread "main" java.lang.NullPointerException // at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011) // at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006) }
源码:
1 2 3 4 5 6 7 8 9
public V put(K key, V value){ return putVal(key, value, false); }
/** Implementation for put and putIfAbsent */ final V putVal(K key, V value, boolean onlyIfAbsent){ if (key == null || value == null) thrownew NullPointerException(); // ... }