Skip to content

Commit d2d73b1

Browse files
KotaiVictormattwhisenhunt
authored andcommitted
Fixing ArrayMap hashCode to work with null values
1 parent 9578ae8 commit d2d73b1

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

google-http-client/src/main/java/com/google/api/client/util/ArrayMap.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ public V setValue(V value) {
416416

417417
@Override
418418
public int hashCode() {
419-
return getKey().hashCode() ^ getValue().hashCode();
419+
K key = getKey();
420+
V value = getValue();
421+
return (key != null ? key.hashCode() : 0) ^ (value != null ? value.hashCode() : 0);
420422
}
421423

422424
@Override

google-http-client/src/test/java/com/google/api/client/util/ArrayMapTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,13 @@ public void testSet() {
100100
} catch (IndexOutOfBoundsException e) {
101101
}
102102
}
103+
104+
public void testHashCode() {
105+
ArrayMap<String, Integer> map = ArrayMap.of();
106+
map.set(0, "a", null);
107+
map.set(1, null, 1);
108+
map.set(2, null, null);
109+
110+
assertTrue(map.hashCode() > 0);
111+
}
103112
}

0 commit comments

Comments
 (0)