为什么钻石操作符不适用于Java 7中的java.util.Collections方法? [英] Why does the diamond operator not work for java.util.Collections methods in Java 7?

查看:356
本文介绍了为什么钻石操作符不适用于Java 7中的java.util.Collections方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 1.7.0_55中,如果我编写了这个字段声明,我得到一个编译错误(不兼容类型):

  private final Map< String,Object> myMap = 
Collections.synchronizedMap(new HashMap<>());

如果我将其更改为:

  private final Map< String,Object> myMap = 
Collections.synchronizedMap(new HashMap< String,Object>());

它编译得很好。 (我在这里使用synchronizedMap作为示例,但对于其他Collections方法也是如此,不可修改*,synchronized *等)

但是为什么菱形运算符不按我期望的那样工作?由于Collections.synchronizedMap()被声明为:

  public static 地图< K,V> synchronizedMap(Map  m){

在我看来,类型参数构造函数的调用必须与字段声明的相同,编译器应该能够根据它推断构造类的类型参数。



我试过寻找JLS中的一个条款说这个语法是不可接受的,但我找不到一个。任何人都可以指向我吗?

解决方案

Java 7中的编译器错误会失败,但它会在Java 8中成功编译。简而言之,编译器的类型推断没有捕获到Java 7中正确的推断类型,但更好的类型推断推断出Java 8中正确的类型。



这个变化是<用于Java 8的JEP(JDK增强建议)101 。

$
$ b


$ b

平滑扩展方法类型推断的范围以支持方法上下文中的(i)推理和(ii) )推断链式调用。

Java 8能够通过带有参数和方法调用链的多个方法调用来推断类型。现在可以通过调用 Collections.synchronizedMap 来从赋值< String,Object> 给该调用的参数中的钻石操作符, new HashMap<>()


In Java 1.7.0_55, if I write this field declaration, I get a compilation error ("incompatible types"):

   private final Map<String,Object> myMap =
       Collections.synchronizedMap(new HashMap<>());

If I change that to read:

   private final Map<String,Object> myMap =
       Collections.synchronizedMap(new HashMap<String,Object>());

It compiles fine. (I'm using synchronizedMap as an example here, but the same is true for other Collections methods, unmodifiable*, synchronized*, etc)

But why does the diamond operator not work as I would expect here? Since Collections.synchronizedMap() is declared as:

public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {

It would seem to me that the type parameters of the constructor invocation must be the same as those of the field declaration, and the compiler should be able to infer the constructed class type parameters based on that.

I tried looking for a clause in the JLS which says this syntax is unacceptable, but I can't find one. Can anyone point me to it?

解决方案

This fails with your compiler error in Java 7, but it compiles successfully in Java 8. In short, the compiler's type inference did not catch the proper inferred types in Java 7, but the better type inference infers the proper types in Java 8.

This change was JEP (JDK Enhancement Proposal) 101 for Java 8.

Summary

Smoothly expand the scope of method type-inference to support (i) inference in method context and (ii) inference in chained calls.

Java 8 is able to infer types through multiple method calls with parameters and method call chaining. It can now determine from the left side of the assignment <String, Object> through the call to Collections.synchronizedMap to the diamond operator in the parameter to that call, new HashMap<>().

这篇关于为什么钻石操作符不适用于Java 7中的java.util.Collections方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆