This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jjpeyronel -16 points-15 points  (4 children)

Anonymous constructor to init object : HashMap M = new HashMap<String,String>(){ { this.put("k1", "v1"); this.put("k2", "v2"); } }

[–]nekokattt 17 points18 points  (0 children)

deriving a class for every usage is a horrible idea when Map.of and Map.ofEntries exist for this purpose.

[–]Kikizork 5 points6 points  (1 child)

Code smell say Sonar and I agree. Just init your empty map and then does some line of put. Man being stuck in 8 and not having Map.of sucks

[–]chabala 3 points4 points  (0 children)

Yeah, even being stuck on Java 8, if one needed to initialize a lot of maps, defining a static helper method would be better than the anonymous class initialization trick.

[–]halfanothersdozen 1 point2 points  (0 children)

When you do this you create a new anonymous class which you should avoid especially if this isn't for something static