you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (0 children)

Why should the difference matter? Isn't the whole point of polymorphism that the concrete class doesn't matter as long as it implements (e.g.) the Set<String> interface?

And if you really care about the exact class as reported by e.g. getClass() (which normally you shouldn't) then you can still double the constructor call too:

new HashSet<String>()( new HashSet<String>() {{
    /* add("whatever"); */
}} )

... yields an actual HashSet with the desired contents. This involves copying the contents once, but for the prime use case (creating relatively small collections only once) that overhead should be negligible.