反转贴图<;字符串,列表<;字符串>&燃气轮机;使用Java8

反转贴图<;字符串,列表<;字符串>&燃气轮机;使用Java8,java,collections,java-8,java-stream,Java,Collections,Java 8,Java Stream,我需要使用Java8将映射转换为map。假设这些值是唯一的。比如说, 输入映射- {"Fruit" -> ["apple","orange"], "Animal" -> ["Dog","Cat"]} 输出映射 {"apple" -> "Fruit", "orange" -> "Fruit", "Dog"->"Animal", "Cat" -> "Animal"} Map outputMap=newhashmap(); 对于(Map.Entry:inputM

我需要使用Java8将
映射转换为
map
。假设这些值是唯一的。比如说,

输入映射-

{"Fruit" -> ["apple","orange"], "Animal" -> ["Dog","Cat"]}
输出映射

{"apple" -> "Fruit", "orange" -> "Fruit", "Dog"->"Animal", "Cat" -> "Animal"}
Map outputMap=newhashmap();
对于(Map.Entry:inputMap.entrySet()){
entry.getValue().forEach(value->outputMap.put(value,entry.getKey());
}   
是这样吗?我们可以使用streams java 8实现这一点吗?

这样做:

public class InverterMap {
    public static void main(String[] args) {

        Map<String, List<String>> mp = new HashMap<String, List<String>>();
        mp.put("Fruit", Arrays.asList("Apple", "Orange"));
        mp.put("Animal", Arrays.asList("Dog", "Cat"));
        System.out.println(mp);  // It returned {Fruit=[Apple, Orange], Animal=[Dog, Cat]}

        Map<String, String> invertMap = mp.entrySet().stream().collect(HashMap::new,
            (m, v) -> v.getValue().forEach(k -> m.put(k, v.getKey())), Map::putAll);

        System.out.println(invertMap);// It returned {Apple=Fruit, Cat=Animal, Orange=Fruit, Dog=Animal}

    }

}
公共类逆变器映射{
公共静态void main(字符串[]args){
Map mp=新的HashMap();
mp.put(“水果”,数组.asList(“苹果”,“橘子”);
mp.put(“动物”,数组.asList(“狗”,“猫”);
System.out.println(mp);//它返回{水果=[苹果,橙色],动物=[狗,猫]}
Map invertMap=mp.entrySet().stream().collect(HashMap::new,
(m,v)->v.getValue().forEach(k->m.put(k,v.getKey()),Map::putAll);
System.out.println(invertMap);//它返回{苹果=水果,猫=动物,橙色=水果,狗=动物}
}
}
阅读了解更多信息。

执行以下操作:

public class InverterMap {
    public static void main(String[] args) {

        Map<String, List<String>> mp = new HashMap<String, List<String>>();
        mp.put("Fruit", Arrays.asList("Apple", "Orange"));
        mp.put("Animal", Arrays.asList("Dog", "Cat"));
        System.out.println(mp);  // It returned {Fruit=[Apple, Orange], Animal=[Dog, Cat]}

        Map<String, String> invertMap = mp.entrySet().stream().collect(HashMap::new,
            (m, v) -> v.getValue().forEach(k -> m.put(k, v.getKey())), Map::putAll);

        System.out.println(invertMap);// It returned {Apple=Fruit, Cat=Animal, Orange=Fruit, Dog=Animal}

    }

}
公共类逆变器映射{
公共静态void main(字符串[]args){
Map mp=新的HashMap();
mp.put(“水果”,数组.asList(“苹果”,“橘子”);
mp.put(“动物”,数组.asList(“狗”,“猫”);
System.out.println(mp);//它返回{水果=[苹果,橙色],动物=[狗,猫]}
Map invertMap=mp.entrySet().stream().collect(HashMap::new,
(m,v)->v.getValue().forEach(k->m.put(k,v.getKey()),Map::putAll);
System.out.println(invertMap);//它返回{苹果=水果,猫=动物,橙色=水果,狗=动物}
}
}
阅读了解更多信息。

您可以尝试这种方式

Map <String, String> updatedMap = new HashMap<>();
oldMap.keySet()
      .forEach(i -> oldMap.get(i)
                          .forEach(k -> updatedMap.put(k, i)));
Map updatemap=newhashmap();
oldMap.keySet()
.forEach(i->oldMap.get(i)
.forEach(k->updatemap.put(k,i));
你可以这样试试

Map <String, String> updatedMap = new HashMap<>();
oldMap.keySet()
      .forEach(i -> oldMap.get(i)
                          .forEach(k -> updatedMap.put(k, i)));
Map updatemap=newhashmap();
oldMap.keySet()
.forEach(i->oldMap.get(i)
.forEach(k->updatemap.put(k,i));

您可以,但它不会很漂亮:
inputMap.stream().flatMap(entry->entry.getValue().stream().map(singleValue->new SimpleEntry(entry.getKey(),singleValue)).collect(toMap(entry::getValue,entry::getKey));
根据您的要求对上述内容的更正将是
inputMap.entrySet().stream().flatMap(entry->entry.getValue().stream().map(singleValue->new AbstractMap.SimpleEntry(singleValue,entry.getKey())))).collect(collector.toMap(map.entry::getValue,map.entry::getKey));
或者
map outputMap=new HashMap();inputMap.forEach((key,value1)->.forEach(value->outputMap.put(value,key));
考虑到key.IMO非流版本的唯一性,在这种情况下看起来更可读。您可以,但它不会很漂亮:
inputMap.stream().flatMap(entry->entry.getValue().stream().map(singleValue->new SimpleEntry.getKey(),singleValue)).collect(toMap(entry::getValue,entry::getKey));
根据您的要求对上述内容的更正为
inputMap.entrySet().stream().flatMap(entry->entry.getValue().stream().map(singleValue->new AbstractMap.SimpleEntry(singleValue,entry.getKey())).collect(Collectors.toMap(Map.Entry::getValue,Map.Entry::getKey));
或者
Map outputMap=new HashMap();inputMap.forEach((key,value1)->value1.forEach(value->outputMap.put(value,key));
考虑到键的唯一性,IMO非流版本在这种情况下看起来更可读。