equals and Hashcode method in Java

最近的面试题:

1.写 equals method

public class Label {
  private String text;
  private int size;
  
  public boolean equals(Object obj){
        if(Null!=obj) {
            if(obj.getClass()==Label.class)
    Label babel = (Label)obj;
    if(NULl!=label.text)
            return (label.text.equals(text) && label.size == size)
else
    return (label.text==null && label.size ==size)  // for label.text = null case
}
return false;
  }


还有个思考就是,如果Label 还有一个variable :private Label label; 要怎么写?

记得考虑是NULL的情况。


2. Hashcode method return 一个常数有什么缺点?


good example :

http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java

//Written by K@stackoverflow
public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(new Person("Subash Adhikari",28));
    people.add(new Person("K",28));
    people.add(new Person("StackOverflow",4));
    people.add(new Person("Subash Adhikari",28));

    for (int i=0;i<people.size()-1;i++){
        for (int y=i+1;y<=people.size()-1;y++){
            System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());
            boolean check = people.get(i).equals(people.get(y));
            System.out.println(check);
        }
    }
}
}

//written by K@stackoverflow
public class Person {
private String name;
private int age;

public Person(String name, int age){
    this.name = name;
    this.age = age;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Person other = (Person) obj;
    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
        return false;
    }
    if (this.age != other.age) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
    hash = 53 * hash + this.age;
    return hash;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}


下面这篇文章不错:

http://tutorials.jenkov.com/java-collections/hashcode-equals.html

http://www.javapractices.com/topic/TopicAction.do?Id=17


1)The hashCode() method of objects is used when you insert them into a HashTableHashMap or HashSet. If you do not know the theory of how a hashtable works internally, you can read about hastables on Wikipedia.org.

When inserting an object into a hastable you use a key. The hash code of this key is calculated, and used to determine where to store the object internally. When you need to lookup an object in a hashtable you also use a key. The hash code of this key is calculated and used to determine where to search for the object.

The hash code only points to a certain "area" (or list, bucket etc) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The hashtable then iterates this area (all keys with the same hash code) and uses the key's equals() method to find the right key. Once the right key is found, the object stored for that key is returned.

So, as you can see, a combination of the hashCode() and equals() methods are used when storing and when looking up objects in a hashtable.

Here are two rules that are good to know about implementing the hashCode() method in your own classes, if the hashtables in the Java Collections API are to work correctly:

  1. If object1 and object2 are equal according to their equals() method, they must also have the same hash code.
  2. If object1 and object2 have the same hash code, they do NOT have to be equal too.

2)hashCode and equals are closely related:
 a) if you override equals, you must override hashCode.
 b) hashCode must generate equal values for equal objects.
 c) equals and hashCode must depend on the same set of "significant" fields. You must use the same set of fields in both of these methods. You are not required to use all fields. For example, a calculated field that depends on others should very likely be omitted from equalsand hashCode.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的影城管理系统,源码+数据库+论文答辩+毕业论文+视频演示 随着现在网络的快速发展,网上管理系统也逐渐快速发展起来,网上管理模式很快融入到了许多生活之中,随之就产生了“小徐影城管理系统”,这样就让小徐影城管理系统更加方便简单。 对于本小徐影城管理系统的设计来说,系统开发主要是采用java语言技术,在整个系统的设计中应用MySQL数据库来完成数据存储,具体根据小徐影城管理系统的现状来进行开发的,具体根据现实的需求来实现小徐影城管理系统网络化的管理,各类信息有序地进行存储,进入小徐影城管理系统页面之后,方可开始操作主控界面,主要功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影信息管理、购票统计管理、系统管理、订单管理,用户前台;首页、电影信息、电影资讯、个人中心、后台管理、在线客服等功能。 本论文主要讲述了小徐影城管理系统开发背景,该系统它主要是对需求分析和功能需求做了介绍,并且对系统做了详细的测试和总结。具体从业务流程、数据库设计和系统结构等多方面的问题。望能利用先进的计算机技术和网络技术来改变目前的小徐影城管理系统状况,提高管理效率。 关键词:小徐影城管理系统;Spring Boot框架,MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值