-1
public Map mystery(Map map1, Map map2) {
Map result = new TreeMap();
for (String s1 : map1.keySet()) {
    if (map2.containsKey(map1.get(s1))) {
        result.put(s1, map2.get(map1.get(s1)));
    }
}
return result;
}

map1={bar=1, baz=2, foo=3, mumble=4}; map2={1=earth, 2=wind, 3=air, 4=fire}

4

2 回答 2

1

对于 map1 方法中的每个键,查看相应的值,并且该值作为 map2 中的键存在,而不是将其放入新的 TreeMap。

考虑一次迭代。map1 有键bar,它的值是1. 现在 map2 具有1value 的关键earth。所以放入新地图的数据是bar:earth等等..

另请注意,由于结果映射是TreeMap元素将按字典顺序存储(因为键是字符串,而 TreeMap 根据键自然顺序按排序顺序存储元素)。

于 2015-02-22T05:16:16.187 回答
0

它正在计算函数的组成map2(map1),并为您提供数量惊人的原始类型警告。

于 2015-02-22T05:12:25.680 回答