三种方法的思维都是遍历一个map的Key,然后2个Map分别取这2个Key值所得到的Value。
#第一种用entry
private void compareMap(Map<String, String> Map01, Map<String, String Map02>){
for (Map.Entry<String, String> entry : Map1.entrySet())
{
String testKey = entry.getKey();
if(Map1.get(testId).equals(Map2.get(testId))){
System.out.println("equals");
}else{
System.out.println("not equals");
}
}
}
#第二种用keyset的方法,把key值存到容器,分别取出对比
private void compareMap(Map<String, String> Map01, Map<String, String Map02>){
Iterator<String> iter = Map1.keySet().iterator();
while (iter.hasNext()) { String testKey = iter.next();
if(Map1.get(testId).equals(Map2.get(testId))){
System.out.println("equals");
}else{
System.out.println("not equals");
}
##第二种用keyset的方法,遍历Key值
public class CompareMap { public static void main(String[] args) { } private void compare(Map<String, String> map1, Map<String, String> map2) { for (String testkey : map1.keySet()) { if(map1.get(testkey).equals(map2.get(testkey))){ System.out.println("equals"); }else{ System.out.println("not equals"); } } }
在main方法调用并传递参就可以了,
PS:如果需要将结果(相同的value、不同的value)写入文件,则可以写一个write方法,在每次打印写入文件,加文件地址参数即可
结果写入文件方法write
public static void write(String file, String valueOfMap1, String valueOfMap2) { try { BufferedWriter input = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file, true))); input.write("Map1Output: " + valueOfMap1 + " "); input.write("Map2Output: " + valueOfmap2 + " "); input.newLine(); input.close(); } catch (IOException e) { e.printStackTrace(); } }