Everywhere I look, I see the typing of Collections done like this:
Set<String> set = new HashSet<String>();
However, I define my Collections like this
Set<String> set = new HashSet();
and I still get the type checking (and my way is just cleaner to look at).
Maybe this has something to do when creating generic collections? But, let's say I just want nothing more than a HashSet of Strings, then is not
Set<String> set = new HashSet();
enough?
You are using a generic class when creating a new collection.
The generic class must get the generic type it encapsulates, otherwise it is considered a Raw Type.
The proper declaration of the collection value, should therefore be:
Set<String> mySet = new HashSet<>();
Your JVM will be able to infer the generic type being used in your HashSet thanks to the declaration on Set<String>
Most IDEs (Eclipse and ItelliJ, for example) will have their linters configured to provide a warning when using a Raw Type class. This warning can be suppressed, but that is considered a bad practice.
References:
Bonus: