Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created April 15, 2013 22:28
Show Gist options
  • Save swankjesse/5391810 to your computer and use it in GitHub Desktop.
Save swankjesse/5391810 to your computer and use it in GitHub Desktop.
PutDuringRemoveEldestEntryTest.java
import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
public class PutDuringRemoveEldestEntryTest extends TestCase {
public void test() {
Map<String, String> map = new LinkedHashMap<String, String>(4) {
@Override protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
if (size() == 4) {
super.put("a", "a"); // This will trigger doubleCapacity().
}
return false;
}
};
map.put("c", "c");
map.put("d", "d");
map.put("e", "e");
// This key has a different hash bucket on a 4-element vs. a 8-element table. But we'll select
// the hash bucket before we resize the table, causing it to be stored in the wrong bucket.
map.put("b", "b");
assertEquals("c", map.get("c"));
assertEquals("d", map.get("d"));
assertEquals("e", map.get("e"));
assertEquals("a", map.get("a"));
assertEquals("b", map.get("b")); // Returns null because the hash bucket is wrong.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment