dav*_*hew 4 java dictionary collect java-stream collectors
我试图将 a 转换List<String>为 a Map<T, String>,映射的值是前一个 中包含的元素List<String>,键是该元素的某个属性String(例如, 的长度String,在这种情况下,T实际上是 an 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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译它时,出现以下编译错误。
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
Run Code Online (Sandbox Code Playgroud)
现在,这很容易解决 - 我只需要通过替换为 来牺牲一些Function::identity可读性(t -> t)。这样做,该类就可以正常编译和运行。
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
Run Code Online (Sandbox Code Playgroud)
{5=apple, 6=banana, 7=coconut}
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 为什么我无法使用Function::identity来获取我需要的数据?查看 的文档Function::identity,我们可以看到这个静态接口方法应该返回 a,Function<T, T>该方法返回它接收的对象。如果我们查看源代码本身,我们可以看到它创建了与我创建的完全相同的 lambda。所以我很困惑为什么第一次尝试失败了。显然,我能够轻松地通过它,但我仍然想知道为什么第一次尝试失败了。
Function::identity用。。。来代替Function.identity()。
前者是方法引用,只有在您有Function对象流并且想要identity对每个对象调用方法时才有效。后者只是一个静态方法调用,它返回Function您作为值映射器参数传递的 a 。
两个注意事项:
each -> each.length()可以写为String::length(方法引用)。当然,如果您愿意,也可以将其保留为 lambda 表达式。Collectors#groupingBy(...).| 归档时间: |
|
| 查看次数: |
1178 次 |
| 最近记录: |