As others have hinted at in this thread, Double-bracket map initialization is a bug. If I see it in any code checked into projects, it gets rejected. There's the somewhat nebulous (as in, YMMV) issue of it being not particularly readable, simply because many java programmers don't recognize the pattern, but that's not why I have a fundamental problem with it.
(A) It's a potential memory and speed hog. This ends up creating a new class, and that class is going to take up extra space. Not just that, it's another subclass, which means hotspot will have a slightly more difficult time optimizing calls into it. This is unlikely to turn into a practical problem but the mere issue of me having to think about it is a problem, and it annoys attempts to profile as well, as each such initialized map is a new class. (B) There are better alternatives. Namely, Guava's MapMaker. (C) The new class is going to frustrate things. Map's equals/hashCode methods don't actually care about subclasses (which also means you can not add equals-significant state to maps as a downside, i.e. a ColouredHashMap, which is a plain hashmap with 1 colour associated with the whole map, cannot be written without having an effectively broken equals method, but I digress) - but it DOES frustrate .getClass(), which is going to weird (and package private!). The primary reason why you shouldn't actually use this is of course (B). -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
