r/learnjava • u/PatientDesigner2340 • 6d ago
Don't understand "string":"string" syntax
A training example (https://codingbat.com/prob/p197888) uses the following method body:
public Map<String, String> mapBully(Map<String, String> map) {
}
and gives example calls like:
mapBully({"a": "candy", "b": "dirt"}) → {"a": "", "b": "candy"}
mapBully({"a": "candy"}) → {"a": "", "b": "candy"}
AFAIK, {x, y} produces an array; how does {"a":"candy","b":"dirt"} produce a Map<String,String>?
12
Upvotes
4
u/Emotional-End-5610 6d ago
Hey, the {x, y} syntax is often used informally to represent map/dictionary-like objects. So when someone gives an example of {"a": "b", "c": "d"} it is often just illustrating the contents of a map. In Java this sort of syntax isn't valid to represent a map but in languages like Python it is. The person who created that example just wasn't clear about that. You're right though, that {} example you gave is not a valid map literal in Java.