Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33de33e

Browse files
replace tests + docs
1 parent 02bdbe0 commit 33de33e

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

‎README.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ Replaces each entry's value with the result of invoking the given
6363
function on that entry until all entries have been processed or the
6464
function throws an exception
6565

66-
* `default boolean replace(K key, V oldValue, V newValue)`
67-
* `default V replace(K key, V value)`
66+
* `default boolean replace(K key, V oldValue, V newValue)` - Replaces the
67+
entry for the specified key only if currently mapped to the specified value.
68+
* `default V replace(K key, V value)` - Replaces the entry for the
69+
specified key only if map contains key.
6870
* `default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)`
6971
* `default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)`
7072
* `default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)`
@@ -116,4 +118,12 @@ We provide tests for above mentioned methods.
116118
Map<String, Integer> counter = new HashMap<>();
117119
118120
counter.replaceAll((k, v) -> ++v)
121+
```
122+
* replace value for given key only for specific oldValue
123+
```
124+
map.replace(1, "oldValue", "newValue");
125+
```
126+
* replace value for given key (map has to contain that key)
127+
```
128+
map.replace(1, "newValue");
119129
```

‎src/test/java/MapModifyingTest.java‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import static org.hamcrest.CoreMatchers.is;
77
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.junit.Assert.assertNull;
89
import static org.junit.Assert.assertTrue;
910

1011
/**
@@ -79,4 +80,80 @@ public void replaceAll() {
7980
assertThat(map.get(2), is("2-updated"));
8081
assertThat(map.get(3), is("3-updated"));
8182
}
83+
84+
@Test
85+
public void replace_key_value_newValue_found() {
86+
Map<Integer, String> map = new HashMap<>();
87+
88+
map.put(1, "old");
89+
90+
map.replace(1, "old", "replaced");
91+
92+
assertThat(map.get(1), is("replaced"));
93+
}
94+
95+
@Test
96+
public void replace_key_value_newValue_notFound() {
97+
Map<Integer, String> map = new HashMap<>();
98+
99+
map.put(1, "1");
100+
101+
map.replace(1, "2", "replaced");
102+
103+
assertThat(map.get(1), is("1"));
104+
}
105+
106+
@Test
107+
public void replace_key_value_newValue_oldNull() {
108+
Map<Integer, String> map = new HashMap<>();
109+
110+
map.put(1, null);
111+
112+
map.replace(1, null, "replaced");
113+
114+
assertThat(map.get(1), is("replaced"));
115+
}
116+
117+
@Test
118+
public void replace_key_value_newValue_newNull() {
119+
Map<Integer, String> map = new HashMap<>();
120+
121+
map.put(1, "1");
122+
123+
map.replace(1, "1", null);
124+
125+
assertThat(map.size(), is(1));
126+
assertNull(map.get(1));
127+
}
128+
129+
@Test
130+
public void replace_key_value_found() {
131+
Map<Integer, String> map = new HashMap<>();
132+
133+
map.put(1, "1");
134+
135+
map.replace(1, "replaced");
136+
137+
assertThat(map.get(1), is("replaced"));
138+
}
139+
140+
@Test
141+
public void replace_key_value_notFound() {
142+
Map<Integer, String> map = new HashMap<>();
143+
144+
map.replace(1, "replaced");
145+
146+
assertTrue(map.isEmpty());
147+
}
148+
149+
@Test
150+
public void replace_key_value_mappedToNull() {
151+
Map<Integer, String> map = new HashMap<>();
152+
153+
map.put(1, null);
154+
155+
map.replace(1, "replaced");
156+
157+
assertThat(map.get(1), is("replaced"));
158+
}
82159
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /