diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableSetTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableSetTest.java index 8a984c9aedac..6dc6f2244d17 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableSetTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableSetTest.java @@ -366,4 +366,12 @@ public void testReusedBuilder() { builder.add("baz"); assertTrue(set.elements != builder.contents); } + + public void testReuseBuilderReducingHashTableSizeWithPowerOfTwoTotalElements() { + ImmutableSet.Builder builder = ImmutableSet.builderWithExpectedSize(6); + builder.add(0); + ImmutableSet unused = builder.build(); + ImmutableSet subject = builder.add(1).add(2).add(3).build(); + assertFalse(subject.contains(4)); + } } diff --git a/guava-gwt/test/com/google/common/collect/ImmutableSetTest_gwt.java b/guava-gwt/test/com/google/common/collect/ImmutableSetTest_gwt.java index cb7180c4685a..4e538bc017d2 100644 --- a/guava-gwt/test/com/google/common/collect/ImmutableSetTest_gwt.java +++ b/guava-gwt/test/com/google/common/collect/ImmutableSetTest_gwt.java @@ -233,6 +233,11 @@ public void testEquals_sameType() throws Exception { testCase.testEquals_sameType(); } +public void testReuseBuilderReducingHashTableSizeWithPowerOfTwoTotalElements() throws Exception { + com.google.common.collect.ImmutableSetTest testCase = new com.google.common.collect.ImmutableSetTest(); + testCase.testReuseBuilderReducingHashTableSizeWithPowerOfTwoTotalElements(); +} + public void testReuseBuilderWithDuplicateElements() throws Exception { com.google.common.collect.ImmutableSetTest testCase = new com.google.common.collect.ImmutableSetTest(); testCase.testReuseBuilderWithDuplicateElements(); diff --git a/guava-tests/test/com/google/common/collect/ImmutableSetTest.java b/guava-tests/test/com/google/common/collect/ImmutableSetTest.java index d5b0abe14d9a..5061f5cd82ea 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableSetTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableSetTest.java @@ -582,4 +582,12 @@ private static long worstCaseQueryOperations(Set set, CallsCounter counter) { } return worstCalls; } + + public void testReuseBuilderReducingHashTableSizeWithPowerOfTwoTotalElements() { + ImmutableSet.Builder builder = ImmutableSet.builderWithExpectedSize(6); + builder.add(0); + ImmutableSet unused = builder.build(); + ImmutableSet subject = builder.add(1).add(2).add(3).build(); + assertFalse(subject.contains(4)); + } } diff --git a/guava/src/com/google/common/collect/ImmutableSet.java b/guava/src/com/google/common/collect/ImmutableSet.java index 7506c303a937..fe8a5f2c7b51 100644 --- a/guava/src/com/google/common/collect/ImmutableSet.java +++ b/guava/src/com/google/common/collect/ImmutableSet.java @@ -813,6 +813,8 @@ SetBuilderImpl review() { int targetTableSize = chooseTableSize(distinct); if (targetTableSize * 2 < hashTable.length) { hashTable = rebuildHashTable(targetTableSize, dedupedElements, distinct); + maxRunBeforeFallback = maxRunBeforeFallback(targetTableSize); + expandTableThreshold = (int) (DESIRED_LOAD_FACTOR * targetTableSize); } return hashFloodingDetected(hashTable) ? new JdkBackedSetBuilderImpl(this) : this; }