繁体   English   中英

转换HashMap <Integer, List<String> &gt;到HashMap <String, HashSet<Integer> &gt;

[英]Convert a HashMap<Integer, List<String>> to HashMap<String, HashSet<Integer>>

我有一个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

如何做呢 ?

这应该有帮助

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)));
  });
});

这是一个示例以及测试:

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方法中。 丑陋的IntegerAndString类是由于Java中缺少元组类型所致。 您可以改用Javatuples之类的库。

仅查看重复链接。

这是我的最终解决方案

 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))));
     }
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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM