java 使用 Java8 流过滤 Map 的键后映射到列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45177184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:34:24  来源:igfitidea点击:

Map to List after filtering on Map's key using Java8 stream

javalistjava-8hashmapcollectors

提问by user1639485

I have a Map<String, List<String>>. I want to transform this map to a List after filtering on the map's key.

我有一个Map<String, List<String>>. 我想在过滤地图的键后将此地图转换为列表。

Example:

例子:

Map<String, List<String>> words = new HashMap<>();
List<String> aList = new ArrayList<>();
aList.add("Apple");
aList.add("Abacus");

List<String> bList = new ArrayList<>();
bList.add("Bus");
bList.add("Blue");
words.put("A", aList);
words.put("B", bList);

Given a key, say, "B"

给定一个键,比如“B”

Expected Output: ["Bus", "Blue"]

This is what I am trying:

这就是我正在尝试的:

 List<String> wordsForGivenAlphabet = words.entrySet().stream()
    .filter(x-> x.getKey().equalsIgnoreCase(inputAlphabet))
    .map(x->x.getValue())
    .collect(Collectors.toList());

I am getting an error. Can someone provide me with a way to do it in Java8?

我收到一个错误。有人可以为我提供一种在 Java8 中执行此操作的方法吗?

回答by Beri

Your sniplet wil produce a List<List<String>>not List<String>.

你的 sniplet 会产生一个List<List<String>>not List<String>

You are missing flatMap, that will convert stream of lists into a single stream, so basically flattens your stream:

您缺少flatMap,它会将列表流转换为单个流,因此基本上会展平您的流:

List<String> wordsForGivenAlphabet = words.entrySet().stream()
    .filter(x-> x.getKey().equalsIgnoreCase(inputAlphabet))
    .map(Map.Entry::getValue)
    .flatMap(List::stream) 
    .collect(Collectors.toList());

You can also add distinct(), if you don't want values to repeat.

distinct()如果不想重复值,也可以添加。

回答by Eugene

Federico is right in his comment, if all you want is to get the values of a certain key (inside a List) why don't you simply do a get(assuming all your keys are uppercase letters already) ?

Federico 在他的评论中是对的,如果您只想获取某个键的值(在 a 内List),为什么不简单地执行 a get(假设您的所有键都已经是大写字母)?

 List<String> values = words.get(inputAlphabet.toUpperCase());

If on the other hand this is just to understand how stream operations work, there is one more way to do it (via java-9 Collectors.flatMapping)

另一方面,如果这只是为了了解流操作的工作原理,还有一种方法可以做到(通过 java-9 Collectors.flatMapping

List<String> words2 = words.entrySet().stream()
            .collect(Collectors.filtering(x -> x.getKey().equalsIgnoreCase(inputAlphabet),
                    Collectors.flatMapping(x -> x.getValue().stream(), 
                          Collectors.toList())));

回答by Vitalii Muzalevskyi

As was previously told after collectyou will get List<List<String>>with only one or zero value in it. You can use findFirstinstead of collectit will return you Optional<List<String>>.

正如之前所说的那样,collect您将List<List<String>>只获得一个或零值。您可以使用findFirst代替collect它会返回给您Optional<List<String>>