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

How to, from a list, filter it and convert to Map<String,String>

我正在尝试从字符串列表中过滤它,并将结果加载到 Map<String,String> 中,其中包含通过测试的字符串和一般原因。

这就是我尝试的原因:

Map<String,String> invalidStrings = null;

invalidStrings = src.getAllSubjects()
                 .stream()
                 .filter(name -> !(name.contains("-value") || name.contains("-key")))
                 .collect(Collectors.toMap(Function.identity(), "Some Reason"));

这就是我得到的:

The method toMap(Function, Function) in the type Collectors is not applicable for the arguments (Function, String)

不知道为什么我做不到...我到处搜索的建议基本上和我做的一样。

这是错误的重要部分:

The method toMap(Function, Function) <--- note the Function, Function

这意味着,toMap 期望第一个参数是您通过 Function.identity() 正确完成的函数,即 v -> v 但您传递的第二个值是 String

值映射器必须是一个函数:

 .collect(Collectors.toMap(Function.identity(), v -> "Some Reason"));

注意 v -> "Some Reason";