Skip to content

Commit d2ee505

Browse files
Merge pull request eugenp#4227 from myluckagain/BAEL-1736
BAEL-1736
2 parents 3c28681 + 4a81b13 commit d2ee505

2 files changed

Lines changed: 302 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.hashtable;
2+
3+
public class Word {
4+
private String name;
5+
6+
public Word(String name) {
7+
this.name = name;
8+
}
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof Word))
18+
return false;
19+
20+
Word word = (Word) o;
21+
return word.getName().equals(this.name) ? true : false;
22+
23+
}
24+
25+
public int hashCode() {
26+
return name.hashCode();
27+
}
28+
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package com.baeldung.hashtable;
2+
3+
import java.util.ConcurrentModificationException;
4+
import java.util.Enumeration;
5+
import java.util.Hashtable;
6+
import java.util.Iterator;
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.*;
10+
import static org.hamcrest.Matchers.*;
11+
import org.junit.Test;
12+
13+
public class HashtableUnitTest {
14+
15+
@Test
16+
public void whenPutAndGet_thenReturnsValue() {
17+
Hashtable<Word, String> table = new Hashtable<Word, String>();
18+
19+
Word word = new Word("cat");
20+
table.put(word, "an animal");
21+
22+
String definition = table.get(word);
23+
24+
assertEquals("an animal", definition);
25+
26+
definition = table.remove(word);
27+
28+
assertEquals("an animal", definition);
29+
}
30+
31+
@Test
32+
public void whenThesameInstanceOfKey_thenReturnsValue() {
33+
Hashtable<Word, String> table = new Hashtable<Word, String>();
34+
Word word = new Word("cat");
35+
table.put(word, "an animal");
36+
String extracted = table.get(word);
37+
assertEquals("an animal", extracted);
38+
}
39+
40+
@Test
41+
public void whenEqualsOverridden_thenReturnsValue() {
42+
Hashtable<Word, String> table = new Hashtable<Word, String>();
43+
Word word = new Word("cat");
44+
table.put(word, "an animal");
45+
String extracted = table.get(new Word("cat"));
46+
assertEquals("an animal", extracted);
47+
}
48+
49+
@Test(expected = NullPointerException.class)
50+
public void whenNullKey_thenException() {
51+
Hashtable<Word, String> table = new Hashtable<Word, String>();
52+
table.put(null, "an animal");
53+
}
54+
55+
@Test(expected = ConcurrentModificationException.class)
56+
public void whenIterate_thenFailFast() {
57+
58+
Hashtable<Word, String> table = new Hashtable<Word, String>();
59+
table.put(new Word("cat"), "an animal");
60+
table.put(new Word("dog"), "another animal");
61+
62+
Iterator<Word> it = table.keySet().iterator();
63+
System.out.println("iterator created");
64+
65+
table.remove(new Word("dog"));
66+
System.out.println("element removed");
67+
68+
while (it.hasNext()) {
69+
Word key = it.next();
70+
System.out.println(table.get(key));
71+
}
72+
}
73+
74+
@Test
75+
public void whenEnumerate_thenNotFailFast() {
76+
77+
Hashtable<Word, String> table = new Hashtable<Word, String>();
78+
table.put(new Word("1"), "one");
79+
table.put(new Word("2"), "two");
80+
table.put(new Word("3"), "three");
81+
table.put(new Word("4"), "four");
82+
table.put(new Word("5"), "five");
83+
table.put(new Word("6"), "six");
84+
table.put(new Word("7"), "seven");
85+
table.put(new Word("8"), "eight");
86+
87+
Enumeration<Word> enumKey = table.keys();
88+
System.out.println("Enumeration created");
89+
table.remove(new Word("1"));
90+
System.out.println("element removed");
91+
while (enumKey.hasMoreElements()) {
92+
Word key = enumKey.nextElement();
93+
System.out.println(table.get(key));
94+
}
95+
}
96+
97+
@Test
98+
public void whenAddElements_thenIterationOrderUnpredicable() {
99+
100+
Hashtable<Word, String> table = new Hashtable<Word, String>();
101+
table.put(new Word("1"), "one");
102+
table.put(new Word("2"), "two");
103+
table.put(new Word("3"), "three");
104+
table.put(new Word("4"), "four");
105+
table.put(new Word("5"), "five");
106+
table.put(new Word("6"), "six");
107+
table.put(new Word("7"), "seven");
108+
table.put(new Word("8"), "eight");
109+
110+
Iterator<Map.Entry<Word, String>> it = table.entrySet().iterator();
111+
while (it.hasNext()) {
112+
Map.Entry<Word, String> entry = it.next();
113+
System.out.println(entry.getValue());
114+
}
115+
}
116+
117+
@Test
118+
public void whenGetOrDefault_thenDefaultGot() {
119+
120+
Hashtable<Word, String> table = new Hashtable<Word, String>();
121+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
122+
Word key = new Word("dog");
123+
String definition;
124+
125+
// old way
126+
/* if (table.containsKey(key)) {
127+
definition = table.get(key);
128+
} else {
129+
definition = "not found";
130+
}*/
131+
132+
// new way
133+
definition = table.getOrDefault(key, "not found");
134+
135+
assertThat(definition, is("not found"));
136+
}
137+
138+
@Test
139+
public void whenPutifAbsent_thenNotRewritten() {
140+
141+
Hashtable<Word, String> table = new Hashtable<Word, String>();
142+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
143+
144+
String definition = "an animal";
145+
// old way
146+
/* if (!table.containsKey(new Word("cat"))) {
147+
table.put(new Word("cat"), definition);
148+
}*/
149+
// new way
150+
table.putIfAbsent(new Word("cat"), definition);
151+
152+
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
153+
}
154+
155+
@Test
156+
public void whenRemovePair_thenCheckKeyAndValue() {
157+
158+
Hashtable<Word, String> table = new Hashtable<Word, String>();
159+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
160+
161+
// old way
162+
/* if (table.get(new Word("cat")).equals("an animal")) {
163+
table.remove(new Word("cat"));
164+
}*/
165+
166+
// new way
167+
boolean result = table.remove(new Word("cat"), "an animal");
168+
169+
assertThat(result, is(false));
170+
}
171+
172+
@Test
173+
public void whenReplacePair_thenValueChecked() {
174+
175+
Hashtable<Word, String> table = new Hashtable<Word, String>();
176+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
177+
178+
String definition = "an animal";
179+
180+
// old way
181+
/* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) {
182+
table.put(new Word("cat"), definition);
183+
}*/
184+
// new way
185+
table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition);
186+
187+
assertThat(table.get(new Word("cat")), is("an animal"));
188+
189+
}
190+
191+
@Test
192+
public void whenKeyIsAbsent_thenNotRewritten() {
193+
194+
Hashtable<Word, String> table = new Hashtable<Word, String>();
195+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
196+
197+
// old way
198+
/* if (!table.containsKey(cat)) {
199+
String definition = "an animal";// calculate
200+
table.put(new Word("cat"), definition);
201+
}
202+
*/
203+
// new way
204+
205+
table.computeIfAbsent(new Word("cat"), key -> "an animal");
206+
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
207+
208+
}
209+
210+
@Test
211+
public void whenKeyIsPresent_thenComputeIfPresent() {
212+
213+
Hashtable<Word, String> table = new Hashtable<Word, String>();
214+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
215+
216+
Word cat = new Word("cat");
217+
// old way
218+
/* if (table.containsKey(cat)) {
219+
String concatination = cat.getName() + " - " + table.get(cat);
220+
table.put(cat, concatination);
221+
}*/
222+
223+
// new way
224+
table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value);
225+
226+
assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal"));
227+
228+
}
229+
230+
@Test
231+
public void whenCompute_thenForAllKeys() {
232+
233+
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
234+
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
235+
for (String animal : animals) {
236+
table.compute(animal, (key, value) -> (value == null ? Integer.valueOf(1) : Integer.valueOf(value) + 1));
237+
}
238+
assertThat(table.values(), hasItems(2, 2, 2, 1));
239+
240+
}
241+
242+
@Test
243+
public void whenInsteadOfCompute_thenMerge() {
244+
245+
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
246+
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
247+
for (String animal : animals) {
248+
table.merge(animal, 1, (oldValue, value) -> (oldValue + value));
249+
}
250+
assertThat(table.values(), hasItems(2, 2, 2, 1));
251+
}
252+
253+
@Test
254+
public void whenForeach_thenIterate() {
255+
256+
Hashtable<Word, String> table = new Hashtable<Word, String>();
257+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
258+
table.put(new Word("dog"), "another animal");
259+
table.forEach((k, v) -> System.out.println(k.getName() + " - " + v)
260+
261+
);
262+
}
263+
264+
@Test
265+
public void whenReplaceall_thenNoIterationNeeded() {
266+
267+
Hashtable<Word, String> table = new Hashtable<Word, String>();
268+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
269+
table.put(new Word("dog"), "another animal");
270+
table.replaceAll((k, v) -> k.getName() + " - " + v);
271+
272+
assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal"));
273+
}
274+
}

0 commit comments

Comments
 (0)