我有两个地图 Map 和 Map2 ,其中的值不断添加。我需要通过比较 Map 中的所有值来检查是否在 Map2 中添加了重复值。我写了一个简单的函数,但它总是给出错误的。<--- 这是我的问题,如果存在重复,它应该是真的。
Map<String, String> map = new HashMap<String, String>();
map.put("1", "Jan");
map.put("2", "Feb");
map.put("3", "Mar");
map.put("4", "Apr");
map.put("5", "May");
map.put("6", "Jun");
List<String> ids = new ArrayList<String>();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iterator.next();
System.out.println("The key is: " + mapEntry.getKey()
+ ",value is :" + mapEntry.getValue());
String value = (String) mapEntry.getValue();
ids.add(value);
//ids.addAll(mapEntry.getValue());
}
Map<String, String> map1 = new HashMap<String, String>();
map1.put("112", "Jan1");
map1.put("22", "Feb");
map1.put("31", "Ma2r");
map1.put("43", "Apr3");
map1.put("51", "May4");
map1.put("63", "Jun5");
Iterator iterator1 = map1.entrySet().iterator();
boolean exists = false;
String value1 = null;
while (iterator1.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iterator1.next();
System.out.println("The key is: " + mapEntry.getKey()
+ ",value is :" + mapEntry.getValue());
value1 = (String) mapEntry.getValue();
//ids.addAll(mapEntry.getValue());
}
for(String id: ids){
System.out.println("ids: " + id);
exists = ids.contains(value1);
}
System.out.println("Value exist?" + exists);