Java 使用流从地图中提取值

Java 使用流从地图中提取值,java,dictionary,filter,Java,Dictionary,Filter,假设我有这个地图: String str = ResourceUtils.getResourceAsString("/myjson.json"); ObjectMapper mapper = new ObjectMapper(); TypeReference rootType = new TypeReference<List<Map<String, Object>>>() { }; List<Map<String, Obj

假设我有这个地图:

String str = ResourceUtils.getResourceAsString("/myjson.json");
ObjectMapper mapper = new ObjectMapper();

TypeReference rootType = new TypeReference<List<Map<String, Object>>>() {
            };
List<Map<String, Object>> root = mapper.readValue(str, rootType);
Map<String, Object> map = root.stream().reduce(new LinkedHashMap<>(), (dest, m2) -> {
        dest.put(m2.get("map").toString(), m2.get("values"));
        return dest;
});
String str=ResourceUtils.getResourceAsString(“/myjson.json”);
ObjectMapper mapper=新的ObjectMapper();
TypeReference根类型=新的TypeReference(){
};
List root=mapper.readValue(str,rootType);
Map Map=root.stream().reduce(新LinkedHashMap(),(dest,m2)->{
dest.put(m2.get(“map”).toString(),m2.get(“value”);
返回目的地;
});
其中str是包含“map”和“values”字段的json文件,map是字符串,values是字符串数组

如何检索包含筛选键值的字符串列表(或数组)? 我现在就在这一点上:

List<String> startWords = map.entrySet()
                .stream()
                .filter(x -> x.getKey().equalsIgnoreCase("x") || x.getKey().equalsIgnoreCase("y"))
List startWords=map.entrySet()
.stream()
.filter(x->x.getKey().equalsIgnoreCase(“x”)| | x.getKey().equalsIgnoreCase(“y”))
我知道最后会有一个.collect(Collectors::toList()),但是如果我在过滤器之后尝试一个平面图,我会有一个流推断错误

谢谢

编辑:预期的输出是如下列表:

List<String> list = new ArrayList<>();
list.put("hello");
list.put("world");
List<String> startWords = map.entrySet()
                .stream()
                .filter(x -> x.getKey().equalsIgnoreCase("x") || x.getKey().equalsIgnoreCase("y"))
                .map(x -> x.getValue())
                .collect(Collectors.toList())
List List=new ArrayList();
列表。放置(“你好”);
上市。卖出(“世界”);

您不想使用
flatMap
,只想使用
map
。应该是这样的:

List<String> list = new ArrayList<>();
list.put("hello");
list.put("world");
List<String> startWords = map.entrySet()
                .stream()
                .filter(x -> x.getKey().equalsIgnoreCase("x") || x.getKey().equalsIgnoreCase("y"))
                .map(x -> x.getValue())
                .collect(Collectors.toList())
List startWords=map.entrySet()
.stream()
.filter(x->x.getKey().equalsIgnoreCase(“x”)| | x.getKey().equalsIgnoreCase(“y”))
.map(x->x.getValue())
.collect(收集器.toList())

当您想要“展平”某些数据结构时,可以使用
flatMap
。例如,如果您可以创建一个列表列表,那么您将使用
flatMap
获取所有元素的单个流。

您必须在收集之前添加一个映射,如下所示:

List<Object> startWords = map.entrySet()
        .stream()
        .filter(x -> x.getKey().equalsIgnoreCase("x") || x.getKey().equalsIgnoreCase("y"))
        .map(x -> x.getValue())
        .collect(Collectors.toList());
List startWords=map.entrySet()
.stream()
.filter(x->x.getKey().equalsIgnoreCase(“x”)| | x.getKey().equalsIgnoreCase(“y”))
.map(x->x.getValue())
.collect(Collectors.toList());

映射取决于您需要什么:getKey或getValue

您想在
startWords
列表中放入什么?我想将过滤键的所有值都放入一个对象或字符串的输入映射值中?既然您想要一个列表,那么应该返回的是键吗?否则,该如何将对象转换为字符串?我的解释中一定忘了什么。我更新了我的帖子,希望它更清楚!好的,我明白了!我还有一个错误,它说收集器不是功能接口。你知道为什么吗?谢谢你的帮助!应该有
collect(Collectors.toList())
而不是my bad。编辑了我的答案,现在应该可以了。我没有看到你的答案,但现在效果很好!非常感谢。@faceID接受对您有效的答案,以便以后更容易为其他人找到答案。好的,我会做的,谢谢。它正在工作,但可能不像我预期的那样,我现在有一个列表,其中索引0是一个数组列表,每个列表中有一个单词,索引1相同,等等。。。有可能有一个单词列表吗?比如List msg=Arrays.asList(“你好”、“世界!”、“你好”、“你在吗”);谢谢你的帮助!我现在有另一个错误,我在上面的评论中回答:)将startsWords的类型更改为List,因为映射包含对象作为值