|
| 1 | +package com.baeldung.hashmapvshashtable; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.ConcurrentModificationException; |
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Hashtable; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Map.Entry; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import com.google.common.collect.Lists; |
| 19 | + |
| 20 | +public class HashmapVsHashtableDifferenceUnitTest { |
| 21 | + |
| 22 | + // null values |
| 23 | + @Test(expected = NullPointerException.class) |
| 24 | + public void givenHashtable_whenAddNullKey_thenNullPointerExceptionThrown() { |
| 25 | + Hashtable<String, String> table = new Hashtable<String, String>(); |
| 26 | + table.put(null, "value"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test(expected = NullPointerException.class) |
| 30 | + public void givenHashtable_whenAddNullValue_thenNullPointerExceptionThrown() { |
| 31 | + Hashtable<String, String> table = new Hashtable<String, String>(); |
| 32 | + table.put("key", null); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenHashmap_whenAddNullKeyAndValue_thenObjectAdded() { |
| 37 | + HashMap<String, String> map = new HashMap<String, String>(); |
| 38 | + map.put(null, "value"); |
| 39 | + map.put("key1", null); |
| 40 | + map.put("key2", null); |
| 41 | + |
| 42 | + assertEquals(3, map.size()); |
| 43 | + } |
| 44 | + |
| 45 | + // fail-fast iterator |
| 46 | + @Test(expected = ConcurrentModificationException.class) |
| 47 | + public void givenHashmap_whenModifyUnderlyingCollection_thenConcurrentModificationExceptionThrown() { |
| 48 | + HashMap<String, String> map = new HashMap<String, String>(); |
| 49 | + map.put("key1", "value1"); |
| 50 | + map.put("key2", "value2"); |
| 51 | + map.put("key3", "value3"); |
| 52 | + |
| 53 | + Iterator<String> iterator = map.keySet().iterator(); |
| 54 | + while(iterator.hasNext()){ |
| 55 | + iterator.next(); |
| 56 | + map.put("key4", "value4"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenHashtable_whenModifyUnderlyingCollection_thenItHasNoEffectOnIteratedCollection() { |
| 62 | + Hashtable<String, String> table = new Hashtable<String, String>(); |
| 63 | + table.put("key1", "value1"); |
| 64 | + table.put("key2", "value2"); |
| 65 | + |
| 66 | + List<String> keysSelected = Lists.newArrayList(); |
| 67 | + Enumeration<String> keys = table.keys(); |
| 68 | + while (keys.hasMoreElements()) { |
| 69 | + String key = keys.nextElement(); |
| 70 | + keysSelected.add(key); |
| 71 | + |
| 72 | + if (key.equals("key1")) { |
| 73 | + table.put("key3", "value3"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + assertEquals(2, keysSelected.size()); |
| 78 | + } |
| 79 | + |
| 80 | + // synchronized map |
| 81 | + @Test |
| 82 | + public void givenHashmap_thenCreateSynchronizedMap() { |
| 83 | + HashMap<String, String> map = new HashMap<String, String>(); |
| 84 | + map.put("key1", "value1"); |
| 85 | + map.put("key2", "value2"); |
| 86 | + map.put("key3", "value3"); |
| 87 | + |
| 88 | + Set<Entry<String, String>> set = map.entrySet(); |
| 89 | + synchronized (map) { |
| 90 | + Iterator<Entry<String, String>> it = set.iterator(); |
| 91 | + while(it.hasNext()) { |
| 92 | + Map.Entry<String, String> elem = (Map.Entry<String, String>)it.next(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + Map<String, String> syncMap = Collections.synchronizedMap(map); |
| 97 | + } |
| 98 | +} |
0 commit comments