0

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?

3
  • @RannLifshitz I don't get warnings. I'll try to figure out why. But, I did see the new HashSet<>() once or twice! thanks!! Commented May 22, 2018 at 2:09
  • Also note that Java 10 will likely have an alternative to repeating the type: var set = new HashSet<String>(); Though this will have a bunch of dangers that are discussed elsewhere.
    – sprinter
    Commented May 22, 2018 at 2:21
  • @sprinter : var is syntactic sugar. I've actually posted a question and answer about it here: stackoverflow.com/questions/49744891/… Commented May 22, 2018 at 2:24

2 Answers 2

7

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:

3
  • I use Netbeans 8.0 and don't get a warning by the linter (and do get the type checking). Commented May 22, 2018 at 2:37
  • @JustSomeone : And when I searched "netbeans 8.0 linter raw-type" the first result I got was : netbeans.org/bugzilla/show_bug.cgi?id=126437 Personally - I would switch to another IDE. Then again, there might be another option: netbeans.org/kb/docs/java/code-inspect.html Commented May 22, 2018 at 2:40
  • I get this warning when using jshell in Mac's terminal. Thus, this right <> should not be omitted when create a new Set. Instead, I could see many tutorials website omits it.
    – Zhou Haibo
    Commented Apr 2, 2021 at 8:54
1

In this particular use case, where you are passing no arguments to the constructor, it makes no difference.

However, if you were using a version of the constructor that took a parameter whose type depends on the type variable, then if you use the raw type constructor, the compiler will not check the type properly.

This incorrect code compiles due to the raw type, and will cause heap pollution:

List<Integer> foo = Arrays.asList(1, 2, 3);
Set<String> set = new HashSet(foo);

Whereas using the proper type argument appropriately prevents it from compiling:

List<Integer> foo = Arrays.asList(1, 2, 3);
Set<String> set = new HashSet<String>(foo);

By the way, you can use the diamond operator in Java 7+ if you don't want to be so verbose:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.