简体   繁体   English

如何从列表中过滤并转换为 Map<String,String>

[英]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.

相关问题 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 如何转换列表<map<string, object> > 列出<map<string, string> > </map<string,></map<string,> - How to Convert List<Map<String, Object>> to List<Map<String, String>> 如何转换列表<map<string,object> > 至 Map<string, string> ? </string,></map<string,object> - How to convert List<Map<String,Object>> to Map<String, String>? 如何将java对象列表转换为Map <String, Map<String, String> &gt; - How to convert a list of java objects to Map<String, Map<String, String>> 如何转换列表<map<string, string> &gt; 列出<map<string, map<string, string> &gt;&gt; </map<string,></map<string,> - how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>> 如何转换列表<String>到地图<String,List<String> &gt; 基于分隔符 - How to convert List<String> to Map<String,List<String>> based on a delimeter 将字符串转换为 Map 列表 - Convert String to List of Map 如何转换地图 <Integer, List<String> &gt;到地图 <Integer, Set<String> &gt; - how to convert Map<Integer, List<String>> to Map<Integer, Set<String>> 如何转换地图<String, List<String> &gt; 设置<String>使用流? - How to convert Map<String, List<String>> to Set<String> using streams? 如何转换清单 <Object> 进入清单 <Map<String,String> &gt;在Java8中 - How to convert List<Object> into List<Map<String,String>> in java8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM