Skip to content

Commit 6598986

Browse files
authored
Create mapAB3.java
Modify and return the given map as follows: if exactly one of the keys "a" or "b" has a value in the map (but not both), set the other to have that same value in the map. mapAB3({"a": "aaa", "c": "cake"}) → {"a": "aaa", "b": "aaa", "c": "cake"} mapAB3({"b": "bbb", "c": "cake"}) → {"a": "bbb", "b": "bbb", "c": "cake"} mapAB3({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "aaa", "b": "bbb", "c": "cake"}
1 parent 695b096 commit 6598986

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Map-1/mapAB3.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Modify and return the given map as follows: if exactly one of the keys "a" or "b" has a value in the map (but not both), set the other to have that same value in the map.
3+
4+
mapAB3({"a": "aaa", "c": "cake"}) → {"a": "aaa", "b": "aaa", "c": "cake"}
5+
mapAB3({"b": "bbb", "c": "cake"}) → {"a": "bbb", "b": "bbb", "c": "cake"}
6+
mapAB3({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "aaa", "b": "bbb", "c": "cake"}
7+
*/
8+
public Map<String, String> mapAB3(Map<String, String> map) {
9+
if(map.containsKey("a") && !map.containsKey("b")) {
10+
map.put("b" , map.get("a"));
11+
}
12+
if(map.containsKey("b") && !map.containsKey("a")) {
13+
map.put("a" , map.get("b"));
14+
}
15+
return map;
16+
}

0 commit comments

Comments
 (0)