如何从Map <string,object =“”>创建一个Guava HashMultiset

时间:2018-08-17 13:11:21

标签: java guava

无法编译:

Map<String, Object> map = new HashMap<String, Object>();
HashMultiset<Map<String, Object>> n1Properties = HashMultiset.create(map);      

enter image description here

1 个答案:

答案 0 :(得分:2)

任何重载的create方法都不接受Map。但是您可以使用addAll方法将转换后的Map添加到Collection

import com.google.common.collect.HashMultiset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SO {

    public static void main(String args[]) {
        Map<String, Object> map = new HashMap<>();
        map.put("foo", "FOO");
        map.put("bar", "BAR");

        HashMultiset<Map<String, Object>> n1Properties = HashMultiset.create();
        n1Properties.addAll(Collections.singleton(map));

        System.out.println(n1Properties);
    }

}

输出:

[{bar=BAR, foo=FOO}]