Java,集合构造函数 [英] Java, Collection constructor
本文介绍了Java,集合构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
之间有什么区别:
TreeMap<String, String> myMap = new TreeMap<>();
和
TreeMap<String, String> myMap = new TreeMap<String,String>();
$ b
谢谢!
Thanks!
推荐答案
它们在java 7中是相同的,其中引入了钻石操作符<>
。在旧版本的java中,钻石操作符将不起作用。
They are the same in java 7 where the diamond operator <>
was introduced. In older versions of java the diamond operator will not work.
钻石操作符为构造函数带来类型推断。对于通用方法的类型推断在java 5及更高版本中可用。在java 7之前,要使用编译器的类型推断创建一个泛型类,你必须使用泛型工厂方法,例如 static< K,T&地图< K,T> createMap()
。
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()
.
这篇关于Java,集合构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文