hashcode()和equals()方法 [英] hashcode() and equals() method

查看:212
本文介绍了hashcode()和equals()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对hashcode()和equals()方法有疑问

so i have a question on hashcode() and equals() method

假设我只是编写一个非常基本的程序来覆盖这两个方法

Let's say I just write a very basic program overridng both the methodes

import java.util.*;

class Employee
{
   private String name;
   private int empid;


   public Employee(String name,int empid)
   {
       this.name=name;
       this.empid=empid;
   }


   public int getEmpid()
   {
       return empid;
   }


   public String getName()
   {
       return name;
   }


   public boolean equals(Object obj)
   {
       System.out.println("equals has just been called...");
       Employee e1=(Employee)obj;
       return ((name.equals(e1.name)) && (empid==e1.empid));
   }


   public int hashCode()
   {
       System.out.println("hashcode called...");
       return empid;
   }

}

然后,假设我写了另一个在HashSet中添加和迭代元素的类

Then, let's say I write another class to add and iterate the elements in HashSet

class Five
{
   public static void main(String args[])
   {
       HashSet hs1=new HashSet();
       hs1.add(new Employee("Alex",25));
       hs1.add(new Employee("Peter",25));
       hs1.add(new Employee("Martin",25));
       hs1.add(new Employee("Alex",25));


       Iterator itr=hs1.iterator();

       while(itr.hasNext())
       {
           Employee e=(Employee)itr.next();
           System.out.println(e.getEmpid()+"\t"+e.getName());
       }


    }

}

现在问题是
,当我尝试再次使用相同的empid添加Alex时equals()总是称为the次

now the question is when i try to add Alex again with the same empid the equals() always called thee times

因为那里没有索引n hashmap所以如果首先用之前添加的Alex检查它将返回true并且不应该为其他两个元素调用(peter和martin)
但等于总是调用3次

as there is no index n hashmap so in case if it first be checked with previously added Alex it will return true and should not be called for the other two elements (peter and martin) but equals always called 3 times

为什么.. ??

同一桶内的对象也有索引.. ??

is objects within same bucket do also have the index..??

推荐答案

等于总是在 hashCode 添加和删除元素时java散列集合中的方法。原因是,如果某个元素已经存在于指定的存储桶中,那么JVM会检查它是否与它尝试放置的元素相同。如果等于返回false,则该元素将添加到同一个存储桶,但位于存储桶列表的末尾。所以现在你只是在同一个桶中没有一个元素而是一个元素列表。

Equals is always called after the hashCode method in a java hashed collection while adding and removing elements. The reason being, if there is an element already at the specified bucket, then JVM checks whether it is the same element which it is trying to put. In case if the equals returns false then the element is added to the same bucket but at the end of list at the bucket. So now you just dont have a single element at the same bucket but a list of elements.

现在在检索元素时,将调用第一个hashCode来到达所需的桶然后使用等号扫描列表以获取所需的元素。

Now while retrieving the element, first hashCode will be called to reach the desired bucket and then the list will be scanned using the equals to fetch the desired element.

hashCode 的理想实现将确保每个桶的列表大小为1.因此,使用O(1)复杂度完成元素的检索。但是如果存储在列表中的多个元素存储在桶中,那么元素的复制将通过O(n)复杂来完成,其中n是列表的大小。

The ideal implemenation of hashCode will make sure the size of list at each bucket is 1. And hence the retrieval of elements is done using O(1) complexity. But if there are mulitple elements stored in the list at a bucket, then the retreival of element will be done by O(n) complexiy, where n is the size of the list.

在HashSet的情况下,没有在存储桶中创建列表,而是在hashcode和equals相同的情况下简单地替换对象。 ist创建行为在hashmap中。

Btw in case of HashSet there is no list created at the bucket, rather the object is simply replaced if hashcode and equals are same. The ist creation behavior is in hashmap.

这篇关于hashcode()和equals()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆