180 |
} |
} |
181 |
|
|
182 |
/** |
/** |
183 |
|
* Called when this entry is accessed via {@link #put(Object, Object)}. |
184 |
|
* This version does nothing, but in LinkedHashMap, it must do some |
185 |
|
* bookkeeping for access-traversal mode. |
186 |
|
*/ |
187 |
|
void access() |
188 |
|
{ |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
* Called when this entry is removed from the map. This version simply |
* Called when this entry is removed from the map. This version simply |
193 |
* returns the value, but in LinkedHashMap, it must also do bookkeeping. |
* returns the value, but in LinkedHashMap, it must also do bookkeeping. |
194 |
* |
* |
347 |
while (e != null) |
while (e != null) |
348 |
{ |
{ |
349 |
if (equals(key, e.key)) |
if (equals(key, e.key)) |
350 |
// Must use this method for necessary bookkeeping in LinkedHashMap. |
{ |
351 |
return e.setValue(value); |
e.access(); // Must call this for bookkeeping in LinkedHashMap. |
352 |
|
Object r = e.value; |
353 |
|
e.value = value; |
354 |
|
return r; |
355 |
|
} |
356 |
else |
else |
357 |
e = e.next; |
e = e.next; |
358 |
} |
} |
381 |
public void putAll(Map m) |
public void putAll(Map m) |
382 |
{ |
{ |
383 |
Iterator itr = m.entrySet().iterator(); |
Iterator itr = m.entrySet().iterator(); |
384 |
|
int msize = m.size(); |
385 |
for (int msize = m.size(); msize > 0; msize--) |
while (msize-- > 0) |
386 |
{ |
{ |
387 |
Map.Entry e = (Map.Entry) itr.next(); |
Map.Entry e = (Map.Entry) itr.next(); |
388 |
// Optimize in case the Entry is one of our own. |
// Optimize in case the Entry is one of our own. |
392 |
put(entry.key, entry.value); |
put(entry.key, entry.value); |
393 |
} |
} |
394 |
else |
else |
395 |
{ |
put(e.getKey(), e.getValue()); |
|
put(e.getKey(), e.getValue()); |
|
|
} |
|
396 |
} |
} |
397 |
} |
} |
398 |
|
|
531 |
public boolean remove(Object o) |
public boolean remove(Object o) |
532 |
{ |
{ |
533 |
// Test against the size of the HashMap to determine if anything |
// Test against the size of the HashMap to determine if anything |
534 |
// really got removed. This is neccessary because the return value |
// really got removed. This is necessary because the return value |
535 |
// of HashMap.remove() is ambiguous in the null case. |
// of HashMap.remove() is ambiguous in the null case. |
536 |
int oldsize = size; |
int oldsize = size; |
537 |
HashMap.this.remove(o); |
HashMap.this.remove(o); |
645 |
void addEntry(Object key, Object value, int idx, boolean callRemove) |
void addEntry(Object key, Object value, int idx, boolean callRemove) |
646 |
{ |
{ |
647 |
HashEntry e = new HashEntry(key, value); |
HashEntry e = new HashEntry(key, value); |
|
|
|
648 |
e.next = buckets[idx]; |
e.next = buckets[idx]; |
649 |
buckets[idx] = e; |
buckets[idx] = e; |
650 |
} |
} |
658 |
* @see #entrySet() |
* @see #entrySet() |
659 |
*/ |
*/ |
660 |
// Package visible, for use in nested classes. |
// Package visible, for use in nested classes. |
661 |
HashEntry getEntry(Object o) |
final HashEntry getEntry(Object o) |
662 |
{ |
{ |
663 |
if (!(o instanceof Map.Entry)) |
if (! (o instanceof Map.Entry)) |
664 |
return null; |
return null; |
665 |
Map.Entry me = (Map.Entry) o; |
Map.Entry me = (Map.Entry) o; |
666 |
int idx = hash(me.getKey()); |
Object key = me.getKey(); |
667 |
|
int idx = hash(key); |
668 |
HashEntry e = buckets[idx]; |
HashEntry e = buckets[idx]; |
669 |
while (e != null) |
while (e != null) |
670 |
{ |
{ |
671 |
if (e.equals(me)) |
if (equals(e.key, key)) |
672 |
return e; |
return equals(e.value, me.getValue()) ? e : null; |
673 |
e = e.next; |
e = e.next; |
674 |
} |
} |
675 |
return null; |
return null; |
710 |
{ |
{ |
711 |
Iterator itr = m.entrySet().iterator(); |
Iterator itr = m.entrySet().iterator(); |
712 |
int msize = m.size(); |
int msize = m.size(); |
713 |
this.size = msize; |
size = msize; |
714 |
|
while (msize-- > 0) |
|
for (; msize > 0; msize--) |
|
715 |
{ |
{ |
716 |
Map.Entry e = (Map.Entry) itr.next(); |
Map.Entry e = (Map.Entry) itr.next(); |
717 |
Object key = e.getKey(); |
Object key = e.getKey(); |
752 |
dest.next = e; |
dest.next = e; |
753 |
} |
} |
754 |
else |
else |
755 |
{ |
buckets[idx] = e; |
|
buckets[idx] = e; |
|
|
} |
|
756 |
|
|
757 |
HashEntry next = e.next; |
HashEntry next = e.next; |
758 |
e.next = null; |
e.next = null; |
805 |
// Read the threshold and loadFactor fields. |
// Read the threshold and loadFactor fields. |
806 |
s.defaultReadObject(); |
s.defaultReadObject(); |
807 |
|
|
808 |
// Read and use capacity. |
// Read and use capacity, followed by key/value pairs. |
809 |
buckets = new HashEntry[s.readInt()]; |
buckets = new HashEntry[s.readInt()]; |
810 |
int len = s.readInt(); |
int len = s.readInt(); |
811 |
|
while (len-- > 0) |
812 |
// Read and use key/value pairs. |
{ |
813 |
for ( ; len > 0; len--) |
Object key = s.readObject(); |
814 |
put(s.readObject(), s.readObject()); |
addEntry(key, s.readObject(), hash(key), false); |
815 |
|
} |
816 |
} |
} |
817 |
|
|
818 |
/** |
/** |