Map-1 > mapShare
prev | next | chance
Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that same value. In all cases remove the key "c", leaving the rest of the map unchanged.
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}
ASKER
public Map<String, String> mapShare(Map<String, String> map) {
String val=map.get("a");
String backup=val;
map.put("b",backup);
// map.put("a","");
map.remove("c");
return map;
}
Expected Runabove fails one test.
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"} {"b": "aaa", "a": "aaa"} OK
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"} {"b": null} X
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"} {"d": "hi", "b": "aaa", "a": "aaa"} OK
mapShare({"b": "1234", "c": "yo", "a": "xyz", "z": "zzz"}) → {"b": "xyz", "a": "xyz", "z": "zzz"} {"b": "xyz", "a": "xyz", "z": "zzz"} OK
mapShare({"d": "ddd", "e": "everything", "b": "1234", "c": "yo", "a": "xyz"}) → {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"} {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"} OK
other tests
X
Correct for more than half the tests
Your progress graph for this problem
ASKER
public Map<String, String> mapShare(Map<String, String> map) {
if(map.containsKey("a")){
String val=map.get("a");
String backup=val;
map.put("b",backup);
// map.put("a","");
map.remove("c");
}
else{
map.remove("c");
}
return map;
}
ASKER
public Map<String, String> mapShare(Map<String, String> map) {
if(map.containsKey("a")){
// String val=map.get("a");
// String backup=val;
map.put("b",map.get("a"));
// map.put("a","");
map.remove("c");
}
// else{
// map.remove("c");
// }
return map;
}
Expected Run
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"} {"b": "aaa", "a": "aaa"} OK
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"} {"b": "xyz", "c": "ccc"} X
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"} {"d": "hi", "b": "aaa", "a": "aaa"} OK
mapShare({"b": "1234", "c": "yo", "a": "xyz", "z": "zzz"}) → {"b": "xyz", "a": "xyz", "z": "zzz"} {"b": "xyz", "a": "xyz", "z": "zzz"} OK
mapShare({"d": "ddd", "e": "everything", "b": "1234", "c": "yo", "a": "xyz"}) → {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"} {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"} OK
other tests
OK
ASKER
public Map < String, String > mapShare(Map < String, String > map) {
if (map.containsKey("a")) {
String val = map.get("a");
String backup = val;
map.put("b", backup);
// map.put("a","");
map.remove("c");
} else {
map.remove("c");
}
return map;
}
ASKER
public Map < String, String > mapShare(Map < String, String > map) {
map.remove("c");
if (map.containsKey("a")) {
// String val=map.get("a");
// String backup=val;
map.put("b", map.get("a"));
// map.put("a","");
//map.remove("c");
}
// else{
// map.remove("c");
// }
return map;
}
ASKER
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
Yes.
This is really a so basic challenge that you should be able to solve this on your own.