java 为什么 <> 运算符不能用于低于 1.7 的源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15845372/
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-10-31 21:01:42  来源:igfitidea点击:

Why <> operator is not allowed for source below 1.7?

java

提问by panipsilos

in a java program , I have the following line of code :

在 java 程序中,我有以下代码行:

HashMap<String, String> query_args = new HashMap<>();

and i receive the following error:

我收到以下错误:

'<>' operator is not allowed for source level below 1.7

低于 1.7 的源代码级别不允许使用“<>”运算符

I tried to change the project compliance and jre to 1.7 but then eclipse could not resolve the imports. I am using jre 1.6.

我试图将项目合规性和 jre 更改为 1.7,但随后 eclipse 无法解析导入。我正在使用 jre 1.6。

Any suggestions?

有什么建议?

回答by Jason

Prior to Java 7 (1.7 here), you needed to do

在 Java 7(此处为 1.7)之前,您需要执行

HashMap<String, String> query_args = new HashMap<String, String>();

Its referred to as the diamond operator, and is not backwards-compatible with earlier versions of Java.

它被称为菱形运算符,并且与早期版本的 Java 不向后兼容。

回答by djechlin

Because that wasn't introduced until Java 7. I really don't know how else to answer that question, besides chronicling the history of Java.

因为它直到 Java 7 才被引入。除了记录 Java 的历史之外,我真的不知道如何回答这个问题。

Regarding imports, you should research why Eclipse cannot resolve whichever imports they are when set to Java 1.7, and if needed post that as a separate question.

关于导入,您应该研究为什么 Eclipse 在设置为 Java 1.7 时无法解析任何导入,如果需要,将其作为单独的问题发布。