关于hashCode和equals方法的有趣采访问题

先决条件: Java中的Equal和Hashcode方法 , 为什么要重写equal和hashcode方法

null

hashCode和equals方法 在Java采访中经常被问到。一般来说,我们没有 覆盖这两种方法 但是,当我们必须覆盖这两种方法时,存在一些场景/需求。其中一种情况是,我们将用户定义的对象存储在使用哈希算法的集合类中。i、 e.哈希表、哈希集和哈希映射。

采访问题: 创建一个包含每个员工地址的映射,并使用员工对象作为键。在此地图中存储一些员工的地址。现在创建一个方法,该方法接受Map和Employee对象作为参数,并返回该员工的地址。

方法1(不重写hashCode和equals方法)

// Java program to create a map of employee
// and address Without overriding
// hashCode and equals methods
import java.util.HashMap;
import java.util.Map;
class Employee {
private int empId;
private String name;
public Employee( int empId, String name)
{
this .empId = empId;
this .name = name;
}
}
class Address {
private int houseNo;
private String streetName;
private String city;
private int pinCode;
public Address( int houseNo, String streetName,
String city, int pinCode)
{
this .houseNo = houseNo;
this .streetName = streetName;
this .city = city;
this .pinCode = pinCode;
}
public String getAddress()
{
return houseNo + ", " + streetName +
", " + city + ", " + pinCode;
}
}
public class Test {
public static String getAddress(Map map, Employee emp)
{
Address adrs = (Address)map.get(emp);
return adrs.getAddress();
}
public static void main(String[] args)
{
Employee emp1 = new Employee( 110 , "Sajid Ali Khan" );
Address adrs1 = new Address( 304 , "Marol Mahrisi" ,
"Mumbai" , 400069 );
Employee emp2 = new Employee( 111 , "Jaspreet Singh" );
Address adrs2 = new Address( 203 , "Seepz" , "Mumbai" ,
400093 );
Map<Employee, Address> map = new HashMap<>();
map.put(emp1, adrs1);
map.put(emp2, adrs2);
System.out.println(Test.getAddress(map, new Employee( 110 ,
"Sajid Ali Khan" )));
}
}


输出:

Exception in thread "main" java.lang.NullPointerException
    at Test.getAddress(Test.java:44)
    at Test.main(Test.java:59)

我们希望输出是雇员的地址,但我们得到了NullPointerException,这是非常直接的。Map中的新雇员(110,“萨吉德·阿里·汗”)和争论中的新雇员(110,“萨吉德·阿里·汗”)是两个不同的例子。因此我们得到了NullPointerException 因为当我们做地图的时候。get(emp),它返回null。

方法2(覆盖hashCode和equals方法)

// Java program to create a map of employee
// and address using overriding
// hashCode and equals methods
import java.util.HashMap;
import java.util.Map;
class Employee {
private int empId;
private String name;
public Employee( int empId, String name)
{
this .empId = empId;
this .name = name;
}
@Override public int hashCode()
{
final int prime = 31 ;
int result = 1 ;
result = prime * result + empId;
result = prime * result +
((name == null ) ? 0 : name.hashCode());
return result;
}
@Override public boolean equals(Object obj)
{
if ( this == obj)
return true ;
if (obj == null )
return false ;
if (getClass() != obj.getClass())
return false ;
Employee other = (Employee)obj;
if (empId != other.empId)
return false ;
if (name == null ) {
if (other.name != null )
return false ;
} else if (!name.equals(other.name))
return false ;
return true ;
}
}
class Address {
private int houseNo;
private String streetName;
private String city;
private int pinCode;
public Address( int houseNo, String streetName,
String city, int pinCode)
{
this .houseNo = houseNo;
this .streetName = streetName;
this .city = city;
this .pinCode = pinCode;
}
public String getAddress()
{
return houseNo + ", " + streetName + ", " +
city + ", " + pinCode;
}
}
public class Test {
public static String getAddress(Map map, Employee emp)
{
Address adrs = (Address)map.get(emp);
return adrs.getAddress();
}
public static void main(String[] args)
{
Employee emp1 = new Employee( 110 , "Sajid Ali Khan" );
Address adrs1 = new Address( 304 , "Marol Mahrisi" ,
"Mumbai" , 400069 );
Employee emp2 = new Employee( 111 , "Jaspreet Singh" );
Address adrs2 = new Address( 203 , "Seepz" , "Mumbai" ,
400093 );
Map<Employee, Address> map = new HashMap<>();
map.put(emp1, adrs1);
map.put(emp2, adrs2);
System.out.println(Test.getAddress(map, new Employee( 110 ,
"Sajid Ali Khan" )));
}
}


输出:

304, Marol Mahrisi, Mumbai, 400069

我们得到了预期的输出,这是因为我们在代码中正确地重写了hashCode和equals方法。当我们做地图的时候。get(emp),它在内部调用我们的重写hashCode方法,该方法产生的hashCode与映射中用作键的employee对象的hashCode相同。

一旦找到正确的bucket,就会调用equals方法并匹配两个Employee对象的所有值。结果,我们得到了雇员对象的正确地址。

本文由 萨吉德·阿里·汗 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享