我现在正在玩哈希图。我试图弄清楚如何比较两个哈希图的值 - 当每个哈希图的值是一个 char 数组时。我环顾四周发现了几种可能有效的方法-我只是不确定如何正确实施。基本上我有两张地图,它们的键不同但值相同。我想遍历其中一个映射并创建一个布尔值,如果它们具有相同的值则返回 true。如果有人可以帮助我解决这个问题,那就太好了。到目前为止,我的代码如下所示:
publipublic class MapExample {
public static void main(String[] args) {
Map<String, char[]> map1 = new HashMap<String, char[]>();
Map<String, char[]> map2 = new HashMap<String, char[]>();
char[] letters1 = new char[3];
letters1[0] = 'a';
letters1[1] = 'b';
letters1[2] = 'c';
char[] letters2 = new char[3];
letters2[0] = 'x';
letters2[1] = 'y';
letters2[2] = 'z';
map1.put("1", letters1);
map1.put("2", letters2);
map1.put("3", letters1);
map2.put("4", letters1);
Set s = map1.entrySet();
Iterator it = s.iterator();
boolean containsValue;
//I've tried this but it produces false (and infinite loop) when maps have the same value
while (it.hasNext()) {
containsValue = map1.equals(map2.values());
System.out.println(containsValue);
}
//I've tried this too, but can't seem to adjust the lists to accept char[]
/*
List<String> values1 = new ArrayList<String>(map1.values());
List<String> values2 = new ArrayList<String>(map2.values());
Collections.sort(values1);
Collections.sort(values2);
boolean mapsHaveEqualValues = values1.equals(values2);*/
}
}