fix a few HashMap bugs

1. HashMap.containsValue only checked one hash bucket, which was
 pretty much useless :)

 2. HashMap.MyIterator.remove was broken in that it failed to
 decrement the size field and it did not update the previousCell field
 properly, which sometimes led to more than one cell being removed.
This commit is contained in:
Joel Dice 2010-11-21 17:26:17 -07:00
parent 19dbc61e9f
commit f9197cb076

View File

@ -149,16 +149,17 @@ public class HashMap<K, V> implements Map<K, V> {
}
public boolean containsValue(Object value) {
if (array != null) {
int index = array.length - 1;
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
if (helper.equal(value, c.getValue())) {
return true;
if (array != null) {
for (int i = 0; i < array.length; ++i) {
for (Cell<K, V> c = array[i]; c != null; c = c.next()) {
if (helper.equal(value, c.getValue())) {
return true;
}
}
}
}
return false;
return false;
}
public V get(Object key) {
@ -450,10 +451,12 @@ public class HashMap<K, V> implements Map<K, V> {
public Entry<K, V> next() {
if (hasNext()) {
if (currentCell != null && currentCell.next() != null) {
previousCell = currentCell;
} else {
previousCell = null;
if (currentCell != null) {
if (currentCell.next() != null) {
previousCell = currentCell;
} else {
previousCell = null;
}
}
currentCell = nextCell;
@ -490,6 +493,7 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
currentCell = null;
-- size;
} else {
throw new IllegalStateException();
}