Avatar ·

Efficiency of Double Brace Initialization in Java: Is It Worth Using?

Hello everyone! I'm a beginner Java developer and recently came across an interesting syntax — Double Brace Initialization. It looks very concise, especially when you need to quickly create and populate a collection.\n\nHere's an example I often see in tutorials:\n\n```java\nMap map = new HashMap<>() {{\n put("key1", "value1");\n put("key2", "value2");\n put("key3", "value3");\n}};\n```\n\nOn one hand, it's very convenient — you don't need to create a separate block of code, declare a variable, and then fill it using `put()`. Everything is done in a single expression. But I'm concerned about how efficient this is. After all, it creates an anonymous inner class, which adds extra overhead.\n\nI read on Stack Overflow that this approach can