List<Sting> list =new ArrayList<>();
和List<Sting> list =new ArrayList<String>();
两者有何区别?
PHPz2017-04-18 09:44:00
No difference. . The data type is specified when List<String>.
JDK 1.7 features added support for type inference.
Enhanced type inference for generic instance creation (diamond)
类型推断是一个特殊的烦恼,下面的代码: Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); 通过类型推断后变成: Map<String, List<String>> anagrams = new HashMap<>(); 这个<>被叫做diamond(钻石)运算符,这个运算符从引用的声明中推断类型。
PHP中文网2017-04-18 09:44:00
There is no difference. Starting from jdk1.7, you can omit the String in the angle brackets on the right.
(The right-hand side is inferred by the generic within the declaration, so no need.)
迷茫2017-04-18 09:44:00
If you express it
List<Sting> list =new ArrayList<>();
List<Sting> list =new ArrayList<String>();
There is no difference between the two. The above ArrayList will also be automatically converted to the generic type of String. The data type that the list can access can only be the String type. And if your first line of code is changed to
List list = new ArrayList();
This makes a big difference. The data type accessed by list is not fixed. You can access String, Integer or even Object types.
阿神2017-04-18 09:44:00
The first one must be written after jdk1.7, otherwise there will be a compilation error. The latter one must be written before 1.7. Of course, it can also be written like this after 1.7