比较Java中的两个 HashMap 对象
在本文中,我们将学习如何比较两个 哈希表 Java中的对象。在Java.util包中存在 HashMap 类。 HashMap是用来存储key-value对的,所以比较HashMap的两个Object有不同的场景。以下是:
- 比较条目
- 比较键
- 比较值
例子:
Input : HashMapA = [a=1, b=2], HashMapB = [a=1, c=2]
Output:
Compare Entry = Not Equal
Compare Keys = Not Equal
Compare values = Equal
1 .比较条目:
条目是一个键值对。我们可以通过将 Entry 与 Map 的equals()方法进行比较来比较两个 HashMap,如果映射具有相同的键值对表示相同的 Entry,则返回 true。
下面是实现:
Java
// Java program to compare two HashMap objects
import java.util.*;
class GFG {
public static void main(String[] args)
{
// New HashMap1
HashMap map1 = new HashMap<>();
// Add Entry to map
map1.put(1, "Akshay");
map1.put(2, "Bina");
map1.put(3, "Chintu");
// New HashMap2
HashMap map2 = new HashMap<>();
// Add Entry to map
map2.put(3, "Chintu");
map2.put(2, "Bina");
map2.put(1, "Akshay");
// New HashMap3
HashMap map3 = new HashMap<>();
// Add Entry to map
map3.put(1, "Akshay");
map3.put(2, "Binod");
map3.put(3, "Chintu");
// Compare map1 and map2
System.out.println("map1 == map2 : "
+ map1.equals(map2));
// Compare map1 and map3
System.out.println("map1 == map3 : "
+ map1.equals(map3));
}
}
Java
// Java program to compare two HashSet keys
import java.util.*;
class GFG {
public static void main(String[] args)
{
// New HashMap1
HashMap map1 = new HashMap<>();
// Add Entry to map
map1.put(1, "Akshay");
map1.put(2, "Bina");
map1.put(3, "Chintu");
// New HashMap2
HashMap map2 = new HashMap<>();
// Add Entry to map
map2.put(3, "Chintu");
map2.put(2, "Bina");
map2.put(1, "Akshay");
// New HashMap3
HashMap map3 = new HashMap<>();
// Add Entry to map
map3.put(1, "Akshay");
map3.put(2, "Binod");
map3.put(4, "Chintu");
// Compare map1 and map2 keys using keySet() and
// equals() method
System.out.println(
"map1 keys == map2 keys : "
+ map1.keySet().equals(map2.keySet()));
// Compare map1 and map3 keys using keySet() and
// equals() method
System.out.println(
"map1 keys == map3 keys : "
+ map1.keySet().equals(map3.keySet()));
}
}
Java
// Java program to compare HashMap values
import java.util.*;
class GFG {
public static void main(String[] args)
{
// New HashMap1
HashMap map1 = new HashMap<>();
// Add Entry to map
map1.put(1, "Akshay");
map1.put(2, "Bina");
map1.put(3, "Chintu");
// New HashMap2
HashMap map2 = new HashMap<>();
// Add Entry to map
map2.put(3, "Chintu");
map2.put(2, "Bina");
map2.put(1, "Akshay");
// New HashMap3
HashMap map3 = new HashMap<>();
// Add Entry to map
map3.put(1, "Akshay");
map3.put(2, "Binod");
map3.put(3, "Shinchain");
// Value set of map1
HashSet set1 = new HashSet<>(map1.values());
// Value set of map2
HashSet set2 = new HashSet<>(map2.values());
// Value set of map3
HashSet set3 = new HashSet<>(map3.values());
// Compare map1 and map2 values using equals()
// method
System.out.println("map1 values == map2 values : "
+ set1.equals(set2));
// Compare map1 and map3 values using equals()
// method
System.out.println("map1 values == map3 values : "
+ set1.equals(set3));
}
}
输出
map1 == map2 : true
map1 == map3 : false
2.比较键:
我们可以通过比较使用keySet()方法获得的键来检查两个 HashMap 对象是否具有相同的键。我们使用 set 的equals()方法来比较键。
下面是实现:
Java
// Java program to compare two HashSet keys
import java.util.*;
class GFG {
public static void main(String[] args)
{
// New HashMap1
HashMap map1 = new HashMap<>();
// Add Entry to map
map1.put(1, "Akshay");
map1.put(2, "Bina");
map1.put(3, "Chintu");
// New HashMap2
HashMap map2 = new HashMap<>();
// Add Entry to map
map2.put(3, "Chintu");
map2.put(2, "Bina");
map2.put(1, "Akshay");
// New HashMap3
HashMap map3 = new HashMap<>();
// Add Entry to map
map3.put(1, "Akshay");
map3.put(2, "Binod");
map3.put(4, "Chintu");
// Compare map1 and map2 keys using keySet() and
// equals() method
System.out.println(
"map1 keys == map2 keys : "
+ map1.keySet().equals(map2.keySet()));
// Compare map1 and map3 keys using keySet() and
// equals() method
System.out.println(
"map1 keys == map3 keys : "
+ map1.keySet().equals(map3.keySet()));
}
}
输出
map1 keys == map2 keys : true
map1 keys == map3 keys : false
3.比较值:
我们可以通过使用values()方法将所有地图值转换为 set 来比较地图对象中包含的值是否相同,然后将值与 等于() 集合的方法。
下面是实现:
Java
// Java program to compare HashMap values
import java.util.*;
class GFG {
public static void main(String[] args)
{
// New HashMap1
HashMap map1 = new HashMap<>();
// Add Entry to map
map1.put(1, "Akshay");
map1.put(2, "Bina");
map1.put(3, "Chintu");
// New HashMap2
HashMap map2 = new HashMap<>();
// Add Entry to map
map2.put(3, "Chintu");
map2.put(2, "Bina");
map2.put(1, "Akshay");
// New HashMap3
HashMap map3 = new HashMap<>();
// Add Entry to map
map3.put(1, "Akshay");
map3.put(2, "Binod");
map3.put(3, "Shinchain");
// Value set of map1
HashSet set1 = new HashSet<>(map1.values());
// Value set of map2
HashSet set2 = new HashSet<>(map2.values());
// Value set of map3
HashSet set3 = new HashSet<>(map3.values());
// Compare map1 and map2 values using equals()
// method
System.out.println("map1 values == map2 values : "
+ set1.equals(set2));
// Compare map1 and map3 values using equals()
// method
System.out.println("map1 values == map3 values : "
+ set1.equals(set3));
}
}
输出
map1 values == map2 values : true
map1 values == map3 values : false