java - PropertyDescriptors 数组上 Stream 的 toMap() 函数中出现编译错误

标签 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));

Eclipse 显示

The method toMap(Function, Function) in the type Collectors is not applicable for the arguments (( pd) -> {}, Function::identity)

这是为什么?

最佳答案

Function.identity()返回一个函数式接口(interface)(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()));

关于java - PropertyDescriptors 数组上 Stream 的 toMap() 函数中出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175786/

相关文章:

java - 简单的java计算器

java - 转换和检索映射的键集

python - 导入字典

java - java 8+ 中是否有一个可以替代 guava PreConditions 的良好字段验证?

java - Java流如何减少累加器参数关联

java - 如何在处理中使用计数器或倒计时清除/重置 Canvas ?

java - 最多 k 次买卖股票的最大利润 [递归到 DP]

java - 如何按其他分隔符拆分字符串

c# - 字典:无法将属性或索引器分配给:它是只读的

Java:使用 Stream API 从原始数组创建列表