r/learnjava • u/PatientDesigner2340 • Oct 19 '26
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>?
13
Upvotes
1
u/AutoModerator Oct 19 '26
Moderator Announcement Read More ยป
} You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
14
u/ZacPack Oct 19 '26
Its just giving an example of the expected output based on the map parameter passed in. It's not syntax, rather just saying {"key1":"value1", "key2":"value2"}.
4
u/Emotional-End-5610 Oct 19 '26
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.
3
u/bowbahdoe Oct 19 '26
What the others is saying is right. If you wanted it to actually work you would do
mapBully(Map.of("a", "candy", "b", "dirt"));2
u/vegan_antitheist Oct 19 '26
That's pseudocode. It doesn't compile. The whole point is that you write code that does compile.