Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to init a generic collection since Java5 + diamond operator

As far as i know, the generics are only useful at compilation time.

Thus it is possible to declare:

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

And then, in this string hashset, to add dogs or anything to that set, without any problem since there is not any check at runtime like for arrays (ArrayStoreException...) (but you may have problems like a classcast when you use that set...

So what i want to say is, we usually instantiate generic collections that way:

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

My question is, why do we put the type of the HashSet, since only the type of the reference of the variable is really important (i think).

I mean, we could simply write:

Set<String> set = new HashSet();

And it would work exactly the same right? So why do we usually write the type during the instantiation? (it's not mandatory)


Edit

I know about the "diamond operator" for type inference but why do we even need it!!! since type inference is already working!

The following code :

            Set<String> set = new HashSet();
            set.add("Test add a string 1");
            set.add("Test add a string 2");
            for ( String s : set ) {
                    System.out.println(s);
            }

Produces the output:

Test add a string 1 Test add a string 2

Test it yourself http://ideone.com/vuiCE

So now you talk about type inference as a Java7 feature, but it is already working for me...

With java7 i will have to replace my

            Set<String> set = new HashSet();

By

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

It's still 2 extra characters for doing exactly the same thing no? (Unless generics are not only compile time with Java7? I don't know)

like image 702
Sebastien Lorber Avatar asked Dec 10 '25 18:12

Sebastien Lorber


1 Answers

And it would work exactly the same right?

In runtime: Yes.

In compile time: No

Your code would be assigning a set containing arbitrary objects, to a variable storing a set containing strings which is bad, and will be frowned upon by the type-checker.

When you write new HashSet() you're using a raw type. I refer to this question regarding it's meaning, and why it should be avoided:

  • What is a raw type and why shouldn't we use it?

In general, you could argue like this:

Set<String> set = new HashSet();

is not far from

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

which in turn is not far from

HashSet tmp = new HashSet();
tmp.add(7);
...
Set<String> set = tmp;

Which still compiles, but is bad for a thousand reasons :)


Java 7 allows you to write

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

though, which tells the compiler to infer the type between the <...>.

Documentation on the "diamond operator":

  • Type Inference for Generic Instance Creation (from oracle.com)
  • Generic Types (from oracle.com)
  • Java 7: Diamond Operator
like image 88
aioobe Avatar answered Dec 12 '25 08:12

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!