java 为什么显式类型参数应该用菱形代替?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33137417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:14:05  来源:igfitidea点击:

Why explicit type argument should be replaced by diamond?

javaandroidandroid-studiodiamond-operator

提问by Dan Chaltiel

I'm using Android Studio and I write this :

我正在使用 Android Studio,我写了这个:

List<Button> buttons = new ArrayList<Button>();

I have this message :

我有这个消息:

Explicit type argument Button should be replaced by <>

显式类型参数 Button 应替换为 <>

I'm curious, why would it be better to use diamond instead ?

我很好奇,为什么用钻石代替会更好?

List<Button> buttons = new ArrayList<>();

EDIT :
I don't agree with the duplicate at all ! I saw that answer before and it compares explicit argument to no argument at all, whereas I compare explicit argument to implicit argument !

编辑:
我根本不同意重复!我之前看到过那个答案,它将显式参数与根本没有参数进行比较,而我将显式参数与隐式参数进行比较!

回答by Sleiman Jneidi

It is less verbose, consider the following

它不那么冗长,请考虑以下内容

Map<String,List<String>> map = new HashMap<String,List<String>>();

vs

对比

Map<String,List<String>> map = new HashMap<>();

I think you would go for the second option

我想你会选择第二个选项