在PropertyDescriptors数组上编译Stream的toMap()函数中的错误

Ton*_*ony 2 java dictionary java-8 java-stream

我有以下代码:

    PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(Mailingadresse.class).getPropertyDescriptors();
    Map<String, PropertyDescriptor> m = Arrays
        .stream(propertyDescriptors)
        .filter(pd -> pd.getReadMethod() != null)
        .collect(Collectors.toMap(pd -> pd.getName().toLowerCase(), Function::identity));
Run Code Online (Sandbox Code Playgroud)

Eclipse显示

收集器类型中的Map(Function,Function)方法不适用于参数((pd) - > {},Function :: identity)

为什么是这样?

Era*_*ran 5

Function.identity()返回一个功能接口(Function),所以你不需要方法引用,需要调用方法:

PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(Mailingadresse.class).getPropertyDescriptors();
Map<String, PropertyDescriptor> m = Arrays
    .stream(propertyDescriptors)
    .filter(pd -> pd.getReadMethod() != null)
    .collect(Collectors.toMap(pd -> pd.getName().toLowerCase(), Function.identity()));
Run Code Online (Sandbox Code Playgroud)