2

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?

4

2 回答 2

5

生成的字节码是一样的。新的菱形运算符纯粹是为了让程序员不必重复指定类型两次。

于 2013-12-14T21:46:35.257 回答
4

消极的。由于类型擦除,菱形运算符(以及一般的泛型)具有与它们一直具有的相同的运行时性能(例如,在运行时Collections只保存对象)。

于 2013-12-14T21:26:04.800 回答