'Set = new HashSet' or 'HashSet = new Hashset'?

Posted by Pureferret on Stack Overflow See other posts from Stack Overflow or by Pureferret
Published on 2012-10-03T09:36:25Z Indexed on 2012/10/03 9:37 UTC
Read the original article Hit count: 420

I'm intialising a HashSet like so in my program:

Set<String> namesFilter = new HashSet<String>();

Is this functionally any different if I initilise like so?

HashSet<String> namesFilter = new HashSet<String>();

I've read this about the collections interface, and I understand interfaces (well, except their use here). I've read this excerpt from Effective Java, and I've read this SO question, but I feel none the wiser.

Is there a best practice in Java, and if so, why? My intuition is that it makes casting to a different type of Set easier in my first example. But then again, you'd only be casting to something that was a collection, and you could convert it by re-constructing it.

© Stack Overflow or respective owner

Related posts about generics

Related posts about collections