[英]How to, from a list, filter it and convert to Map<String,String>
I am trying to, from a list of Strings, filter it, and load the results into a Map<String,String>
of with the strings that passed through the test and a generic reason.我试图从字符串列表中过滤它,并将结果加载到
Map<String,String>
中,其中包含通过测试的字符串和一般原因。
This is why I am trying:这就是我尝试的原因:
Map<String,String> invalidStrings = null;
invalidStrings = src.getAllSubjects()
.stream()
.filter(name -> !(name.contains("-value") || name.contains("-key")))
.collect(Collectors.toMap(Function.identity(), "Some Reason"));
This is what I am getting:这就是我得到的:
The method toMap(Function, Function) in the type Collectors is not applicable for the arguments (Function, String)
类型 Collectors 中的 toMap(Function, Function) 方法不适用于参数 (Function, String)
No idea why I am not being able to do this... Everywhere I search the suggestions are basically the same as I am doing.不知道为什么我不能这样做......我搜索的每个地方的建议都与我所做的基本相同。
This is the important part of the error:这是错误的重要部分:
The method toMap(Function, Function) <--- note the
Function, Function
toMap(Function, Function)方法<---注意
Function, Function
This means, toMap
expects the first argument to be a function which you've done correctly via Function.identity()
ie v -> v
but the second value you've passed is a String
这意味着,
toMap
期望第一个参数是您通过Function.identity()
正确完成的函数,即v -> v
但您传递的第二个值是一个String
Rather the value mapper must be a function:相反,值映射器必须是一个函数:
.collect(Collectors.toMap(Function.identity(), v -> "Some Reason"));
note the v -> "Some Reason"
;注意
v -> "Some Reason"
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.