Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从与其他列表关联的映射中获取密钥_Java_Collections_Java 8 - Fatal编程技术网

Java 如何从与其他列表关联的映射中获取密钥

Java 如何从与其他列表关联的映射中获取密钥,java,collections,java-8,Java,Collections,Java 8,我需要通过将List与map的键相关联来创建一个映射,如下所示 Input Map< String,List< String>> - {"Fruit" -> ["apple","orange"], "Animal" -> ["cat","dog"]} Input List< String> - {"apple","dog","xyzzy"} output map as {"Fruit" -> "apple","Animal" ->

我需要通过将List与map>的键相关联来创建一个映射,如下所示

 Input Map< String,List< String>>  - {"Fruit" -> ["apple","orange"], "Animal" -> ["cat","dog"]}

Input List< String> - {"apple","dog","xyzzy"}

output map as {"Fruit" -> "apple","Animal" -> "dog"} by discarding unmatching entries like "xyzzy" in this case" 


```
Map <String, String> outputMap = new HashMap<>();
Map <String, String> tempMap = new HashMap<>();

for (Map.Entry<String, List<String>> entry : inputMap.entrySet()) {
      entry.getValue().forEach(value -> tempMap.put(value, entry.getKey()));
   }    

 inputList.forEach(value ->
            {   
             if(tempMap.get(value)!=null)
             outputMap.put(tempMap.get(value),value); });
            }
有没有一种更简洁、更精确的方法来实现这一点?

因为您的输出类型是Map,所以无法获得输出

{"Fruit" -> "apple","Animal" -> "dog", "Animal" -> "cat"}
字符串动物是键,不能复制

但是,如果要过滤输入映射并检查包含列表,可以执行以下操作:

final Map<String, List<String>> inputMap = new HashMap<>();
inputMap.put("Fruit", asList("apple", "orange"));
inputMap.put("Animal", asList("cat", "dog"));

final List<String> list = asList("apple", "dog", "cat");

final Map<String, List<String>> outputMap = inputMap.entrySet().stream()
        .filter(e -> e.getValue().stream().anyMatch(list::contains))
        .collect(toMap(e -> e.getKey(), e -> e.getValue().stream().filter(list::contains).collect(toList())));

outputMap.forEach((k, v) -> System.out.printf("%s: %s%n", k, v));
final Map<String, List<String>> outputMap = inputMap.entrySet().stream()
        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().stream().filter(list::contains).collect(toList())))
        .filter(e -> !e.getValue().isEmpty())
        .collect(toMap(e -> e.getKey(), e -> e.getValue()));
双重过滤遍历将列出两次,这取决于输入贴图的方式。无论如何,不需要重新分组;如果输入匹配成高百分比,您可以执行以下操作:

final Map<String, List<String>> inputMap = new HashMap<>();
inputMap.put("Fruit", asList("apple", "orange"));
inputMap.put("Animal", asList("cat", "dog"));

final List<String> list = asList("apple", "dog", "cat");

final Map<String, List<String>> outputMap = inputMap.entrySet().stream()
        .filter(e -> e.getValue().stream().anyMatch(list::contains))
        .collect(toMap(e -> e.getKey(), e -> e.getValue().stream().filter(list::contains).collect(toList())));

outputMap.forEach((k, v) -> System.out.printf("%s: %s%n", k, v));
final Map<String, List<String>> outputMap = inputMap.entrySet().stream()
        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().stream().filter(list::contains).collect(toList())))
        .filter(e -> !e.getValue().isEmpty())
        .collect(toMap(e -> e.getKey(), e -> e.getValue()));

一个不直接相关的问题是,为了提高性能,最好将过滤器列表定义为一个集合。

地图中不能有重复的键。所以这个问题是无效的。这里怎么能有两个动物的钥匙?
final Map<String, List<String>> outputMap = inputMap.entrySet().stream()
        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().stream().filter(list::contains).collect(toList())))
        .filter(e -> !e.getValue().isEmpty())
        .collect(toMap(e -> e.getKey(), e -> e.getValue()));