---
title: "Discussion on \"Avoid Java double brace initialization\" | Hashnode"
description: "Discussion on \"Avoid Java double brace initialization\". TL;DR\nAvoid doing this:\nnew HashMap<String, String>() {{\n  put(\"key\", value);\n}};\n\nLeak Trace\nI was recently looking at the following leak trace from LeakCanary:\n┬───\n│ GC Root: Global variable in native code\n│\n├─ com.bugsnag.android.AnrPlugin instan..."
url: "https://hashnode.com/posts/avoid-java-double-brace-initialization/649b4d587ce4b45469299ccf"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Avoid Java double brace initialization](https://blog.p-y.wtf/avoid-java-double-brace-initialization?utm_source=hashnode&utm_medium=feed)

TL;DR Avoid doing this: new HashMap<String, String>() {{ put("key", value); }}; Leak Trace I was recently looking at the following leak trace from LeakCanary: ┬─── │ GC Root: Global variable in native code │ ├─ com.bugsnag.android.AnrPlugin instan...

Jun 27, 2023 · 4 min read · blog.p-y.wtf  
Read the article

-   [#android](https://hashnode.com/tag/android)
-   [#java](https://hashnode.com/tag/java)
-   [#memory-leak](https://hashnode.com/tag/memory-leak)
-   [#patterns](https://hashnode.com/tag/patterns)

## Written by

[Pierre-Yves Ricau](https://hashnode.com/@py)

@py · San Francisco

  
[View profile](https://hashnode.com/@py)

Deep dives

My pronouns are he / him. Android Engineer at Square / Block. A few things I enjoy: Publishing Open Source libraries (LeakCanary, Curtains, Radiography, square/logcat) Rollerblading Writing technical blogs (previously on dev.to/pyricau) Cooking Giving tech talks Reading sci-fi

## Responses 1

Markdown supported · ⌘↵ to post

[Svyatoslav Starkov](https://hashnode.com/@Svyat)

[Aug 10, 2023](https://hashnode.com/posts/avoid-java-double-brace-initialization/649b4d587ce4b45469299ccf/comment/64d55143f207cb2b50323982)

To avoid leaks one can wrap double brace initialization into a static function. Not pretty, but should work.
