转换HashMap <integer,list <string =“”>&gt;到HashMap <string,hashset <integer =“”>&gt;

时间:2017-05-30 13:21:48

标签: java

我输入HashMap<Integer, List<String>>,例如:

3  :  a, b, c
6  :  b, c
9  :  a, c
12 :  b ,c

我希望将其转换为HashMap<String, HashSet<Integer>>,例如

a : 3, 9
b : 3, 6, 12
c : 3, 6, 9, 12

怎么做?

4 个答案:

答案 0 :(得分:1)

这应该有帮助

Map<Integer, List<String>>  initMap = new HashMap<>();

initMap.put(3, new ArrayList<String>(Arrays.asList("a", "b", "c")));
initMap.put(6, new ArrayList<String>(Arrays.asList("b", "c")));
initMap.put(9, new ArrayList<String>(Arrays.asList("a", "c")));
initMap.put(12, new ArrayList<String>(Arrays.asList("b", "c")));

Map<String, Set<Integer>> finalMap = new HashMap<>();

initMap.forEach((key, values) -> {
  values.forEach(val -> {
    Object o = finalMap.containsKey(val) ? 
             finalMap.get(val).add(key) : 
             finalMap.put(val, new HashSet<Integer>(Arrays.asList(key)));
  });
});

答案 1 :(得分:1)

这是一个例子,还有测试:

data.frame

实际逻辑采用import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; import org.junit.Assert; import org.junit.Test; public class SoTests { @Test public void so01() { HashMap<Integer, List<String>> input = new HashMap<>(4); input.put(3, Arrays.asList("a", "b", "c")); input.put(6, Arrays.asList("b", "c")); input.put(9, Arrays.asList("a", "c")); input.put(12, Arrays.asList("b", "c")); HashMap<String, HashSet<Integer>> output = process(input); HashMap<String, HashSet<Integer>> expectedOutput = new HashMap<>(3); expectedOutput.put("a", new HashSet<>(Arrays.asList(3, 9))); expectedOutput.put("b", new HashSet<>(Arrays.asList(3, 6, 12))); expectedOutput.put("c", new HashSet<>(Arrays.asList(3, 6, 9, 12))); Assert.assertEquals(expectedOutput, output); } private HashMap<String, HashSet<Integer>> process(HashMap<Integer, List<String>> input) { return input.entrySet().stream() // .flatMap(entry -> entry.getValue().stream().map(s -> new IntegerAndString(entry.getKey(), s))) // .collect(Collectors.groupingBy(IntegerAndString::getString, HashMap::new, // Collectors.mapping(IntegerAndString::getInteger, Collectors.toCollection(HashSet::new)))); } private static class IntegerAndString { private final Integer integer; private final String string; IntegerAndString(Integer integer, String string) { this.integer = integer; this.string = string; } Integer getInteger() { return integer; } String getString() { return string; } } } 方法。丑陋的process类是由于Java中缺少元组类型。您可以使用某些类似javatuples的库。

答案 2 :(得分:1)

只看重复链接。

这是我的最终解决方案

 private HashMap<String, HashSet<Integer>> process(HashMap<Integer, List<String>> input) {
     return input.entrySet().stream()
             .flatMap(entry -> entry.getValue().stream().map(s -> new SimpleEntry<>(entry.getKey(), s)))
             .collect(Collectors.groupingBy(SimpleEntry::getValue, HashMap::new,
                 Collectors.mapping(SimpleEntry::getKey, Collectors.toCollection(HashSet::new))));
     }

答案 3 :(得分:0)

Map<Integer, List<String>> intHM = new HashMap<Integer, List<String>>();

    intHM.put(3, new ArrayList<String>(Arrays.asList("a","b","c")));

    intHM.put(6, new ArrayList<String>(Arrays.asList("b","c")));

    intHM.put(9, new ArrayList<String>(Arrays.asList("a","c")));

    intHM.put(12, new ArrayList<String>(Arrays.asList("b","c")));

    Map<String,List<Integer>> StringHM = new HashMap<String, List<Integer>>();

    Iterator<Entry<Integer,List<String>>> iterator = intHM.entrySet().iterator();

    while (iterator.hasNext()) {

        Map.Entry<Integer,List<String>> entry = (Map.Entry<Integer,List<String>>) iterator.next();

        for(String str: entry.getValue())
        {

            if (StringHM.containsKey(str))
                StringHM.get(str).add(entry.getKey());
            else
                StringHM.put(str, new ArrayList<Integer>(Arrays.asList(entry.getKey())));

        }
    }

    Iterator<Entry<String,List<Integer>>> StringIterator = StringHM.entrySet().iterator();

    while (StringIterator.hasNext()) {

        Map.Entry<String,List<Integer>> entry = (Map.Entry<String,List<Integer>>) StringIterator.next();

        System.out.print(entry.getKey()+" ");

        for(Integer i: entry.getValue())

            System.out.print(i+" ");

        System.out.println();
    }