How to initialize a HashMap in Java?

To initialize a HashMap in Java, use the double-brace syntax.

Here's how you do it:

var hashMap = new java.util.HashMap<>() {{
  put("Hello", "World!");
  put("Another", "One");
}};

System.out.println(hashMap); // {Hello=World!, Another=One}