Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set without Datatype mentioned

Tags:

java

Is it fine to write following?

Set<Integer> integs = new HashSet<>();

Inside <>, is it fine to leave it as empty?

like image 369
hatellla Avatar asked Apr 17 '15 10:04

hatellla


1 Answers

As from Java 7, the compiler will infer the data type of the hash set without the need to write it twice.

Note though that there are some scenarios where the compiler might fail to infer the type, so you could get compilation errors for more complex scenarios.

More of that here.

like image 149
npinti Avatar answered Oct 13 '22 01:10

npinti