Avatar ·

How to initialize HashMap directly (literal way) in Java?

Hello colleagues! I've been writing Java recently and encountered a problem. I need to create a small dictionary (HashMap) with several key-value pairs directly in code, without the lengthy procedure of adding elements. In Python, I'm used to doing it like this: `my_dict = {"key1": "value1", "key2": "value2"}` — it's convenient and clear. In Java, as far as I understand, there is no such "literal" notation for HashMap. Every time I have to write verbose code: ```java HashMap map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); ``` This is fine for three elements, but when there are 10–15 of them, the code becomes cumbersome. I've heard there is some "double brace initialization", but I've read that it's bad practice. I've also seen menti