gpt4 book ai didi

java - Function::identity 在 Collectors.toMap 中不起作用

转载 作者:行者123 更新时间:2023-12-05 03:24:11 32 4
gpt4 key购买 nike

<分区>

我正在尝试转 List<String>进入 Map<T, String> , map 的值是包含在前一个 List<String> 中的元素,关键是那个 String 的一些属性(例如,String 的长度,在这种情况下,T 实际上是一个 Integer)。

我第一次尝试这样做。这是我上面提到的示例的实现。我想要 String 的长度成为关键,String本身就是值(value)。我想使用 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) .这样做,该类编译并运行得很好。

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 的文档, 我们可以看到这个静态接口(interface)方法应该返回一个 Function<T, T>返回它接收到的对象。如果我们查看源代码本身,我们可以看到它创建了 exact same lambda。我做到了。所以我很困惑为什么第一次尝试失败了。显然,我可以轻松通过它,但我仍然想知道第一次尝试失败的原因。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com