Java, Collection constructor

陌路散爱 提交于 2019-12-02 07:46:33

问题


is there any difference between :

TreeMap<String, String> myMap = new TreeMap<>();

and

TreeMap<String, String> myMap = new TreeMap<String,String>();

Thanks!


回答1:


They are the same in java 7 where the diamond operator <> was introduced. In older versions of java the diamond operator will not work.

The diamond operator brings type inference to constructors. Type inference on generic methods is available in java 5 and higher. Prior to java 7, to create a generic class using the compiler's type inference you had to use generic factory methods like static <K,T> Map<K,T> createMap().




回答2:


First one will only work in Java 7, the second one from Java 5+




回答3:


No difference at all..! Its just a language construct. <> is newly introduced operator known as diamond operator from java 7.



来源:https://stackoverflow.com/questions/12857129/java-collection-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!