Function::identity 不适用于 Collectors.toMap

Function::identity doesn't work in Collectors.toMap

我正在尝试将 List<String> 变成 Map<T, String>,映射的值是包含在前一个 List<String> 中的元素,键是String(例如,String 的长度,在这种情况下,T 实际上是 Integer)。

我第一次尝试这样做。这是我上面提到的示例的实现。我希望 String 的长度作为键,而 String 本身作为值。我想使用 Function::identity 函数来明确指定该值是 String 本身。

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SOQ_ME_20220522_fail
{

   public static void main(String[] args)
   {
   
      final List<String> list = List.of("apple", "banana", "coconut");
      
      var result = 
         list.stream()
            .collect(
               Collectors.toMap(
                  each -> each.length(),
                  Function::identity
               )
            )
         ;

      System.out.println(result);
   
   }
   
}

但是,当我尝试编译它时,出现了以下编译错误。

SOQ_ME_20220522_fail.java:15: error: no suitable method found for toMap((each)->ea[...]gth(),Function::identity)
               Collectors.toMap(
                         ^
    method Collectors.<T#1,K#1,U#1>toMap(Function<? super T#1,? extends K#1>,Function<? super T#1,? extends U#1>) is not applicable
      (cannot infer type-variable(s) T#1,K#1,U#1
        (argument mismatch; unexpected static method <T#2>identity() found in unbound lookup))
    method Collectors.<T#3,K#2,U#2>toMap(Function<? super T#3,? extends K#2>,Function<? super T#3,? extends U#2>,BinaryOperator<U#2>) is not applicable
      (cannot infer type-variable(s) T#3,K#2,U#2
        (actual and formal argument lists differ in length))
    method Collectors.<T#4,K#3,U#3,M>toMap(Function<? super T#4,? extends K#3>,Function<? super T#4,? extends U#3>,BinaryOperator<U#3>,Supplier<M>) is not applicable
      (cannot infer type-variable(s) T#4,K#3,U#3,M
        (actual and formal argument lists differ in length))
  where T#1,K#1,U#1,T#2,T#3,K#2,U#2,T#4,K#3,U#3,M are type-variables:
    T#1 extends Object declared in method <T#1,K#1,U#1>toMap(Function<? super T#1,? extends K#1>,Function<? super T#1,? extends U#1>)
    K#1 extends Object declared in method <T#1,K#1,U#1>toMap(Function<? super T#1,? extends K#1>,Function<? super T#1,? extends U#1>)
    U#1 extends Object declared in method <T#1,K#1,U#1>toMap(Function<? super T#1,? extends K#1>,Function<? super T#1,? extends U#1>)
    T#2 extends Object declared in method <T#2>identity()
    T#3 extends Object declared in method <T#3,K#2,U#2>toMap(Function<? super T#3,? extends K#2>,Function<? super T#3,? extends U#2>,BinaryOperator<U#2>)
    K#2 extends Object declared in method <T#3,K#2,U#2>toMap(Function<? super T#3,? extends K#2>,Function<? super T#3,? extends U#2>,BinaryOperator<U#2>)
    U#2 extends Object declared in method <T#3,K#2,U#2>toMap(Function<? super T#3,? extends K#2>,Function<? super T#3,? extends U#2>,BinaryOperator<U#2>)
    T#4 extends Object declared in method <T#4,K#3,U#3,M>toMap(Function<? super T#4,? extends K#3>,Function<? super T#4,? extends U#3>,BinaryOperator<U#3>,Supplier<M>)
    K#3 extends Object declared in method <T#4,K#3,U#3,M>toMap(Function<? super T#4,? extends K#3>,Function<? super T#4,? extends U#3>,BinaryOperator<U#3>,Supplier<M>)
    U#3 extends Object declared in method <T#4,K#3,U#3,M>toMap(Function<? super T#4,? extends K#3>,Function<? super T#4,? extends U#3>,BinaryOperator<U#3>,Supplier<M>)
    M extends Map<K#3,U#3> declared in method <T#4,K#3,U#3,M>toMap(Function<? super T#4,? extends K#3>,Function<? super T#4,? extends U#3>,BinaryOperator<U#3>,Supplier<M>)
1 error

现在,这很容易解决 - 我只需要牺牲一些可读性,将 Function::identity 替换为 (t -> t)。这样做,class 编译并运行得很好。

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SOQ_ME_20220522_success
{

   public static void main(String[] args)
   {
   
      final List<String> list = List.of("apple", "banana", "coconut");
      
      var result = 
         list.stream()
            .collect(
               Collectors.toMap(
                  each -> each.length(),
                  t -> t
               )
            )
         ;
         
      System.out.println(result);
   
   }
   
}
{5=apple, 6=banana, 7=coconut}

我的问题是 - 为什么我无法使用 Function::identity 来获取我需要的数据?查看我所做的 Function::identity, we can see that this static interface method is supposed to return a Function<T, T> that returns the object it receives. And if we go to the source code itself, we can see that it creates the exact same lambda 的文档。所以我很困惑为什么第一次尝试失败了。明明很容易就过去了,但我还是想知道为什么第一次尝试失败了。

Function::identity 替换为 Function.identity()

前者是一个方法引用,仅当您有一个 Function 对象流并且您想对每个对象调用 identity 方法时才有效。后者只是一个静态方法调用,其中 returns 一个 Function 作为值映射器参数传递。

两个注意事项:

  1. each -> each.length() lambda 表达式可以写成String::length(方法引用)。如果你愿意,你当然可以将它保留为 lambda 表达式。
  2. 如果多个元素可以映射到同一个键(例如,在您的示例中,如果多个字符串可以具有相同的长度),则考虑提供 merge function or using Collectors#groupingBy(...).