Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diamond Operator performance

I'm wondering if there are any known performance differences in terms of the Java 7 diamond operator versus the language construct for previous versions of Java.

Basically, is it faster to use this:

List<String> myList = new ArrayList<>()
Map<String, Integer> myMap = new HashMap<>()

or to use this:

List<String> myList = new ArrayList<String>() 
Map<String, Integer> myMap = new HashMap<String, Integer>()

Are they the same speed?

like image 680
ecbrodie Avatar asked Dec 19 '25 19:12

ecbrodie


2 Answers

The generated bytecode is the same. The new diamond operator is purely implemented to save programmers from having to redundantly specify the type twice.

like image 125
Birb Avatar answered Dec 22 '25 08:12

Birb


Negative. Due to type erasure, the diamond operator (and generics in general) have the same run-time performance as they always have (e.g. at run-time Collections just hold Object(s)).

like image 35
Elliott Frisch Avatar answered Dec 22 '25 09:12

Elliott Frisch



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!