使用HashMap的hashCode()方法 [英] hashCode() method using HashMap

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

问题描述

如果我要定制 HashMap ,我必须重写 hashCode()方法吗?



UPD:例如:

  import java.util.HashMap; 
import java.util.Objects;
$ b $ **
*
* @author dolgopolov.a
* /
公共类SaltEntry {

私人长时间;
私人字符串盐;
$ b $ **
* @返回时间
* /
public long getTime(){
return time;
}

/ **
* @param时间设置
* /
的时间public void setTime(long time){
this.time = time;

$ b $ **
* @返回salt
* /
public String getSalt(){
return salt;
}

/ **
* @param salt salt设置
* /
public void setSalt(String salt){
这盐=盐;
}

public boolean equals(SaltEntry o){
if(salt.equals(o.getSalt())&& time == o.getTime()) {
返回true;
} else {
return false;



public int hashCode(){
return Objects.hash(String.valueOf(salt + time));

$ b $ public static void main(String [] args){
SaltEntry se1 = new SaltEntry(),se2 = new SaltEntry();
se1.setSalt(1);
se2.setSalt(1);
se1.setTime(1);
se2.setTime(1);
HashMap< String,SaltEntry> hm = new HashMap< String,SaltEntry>();
hm.put(1,se1);
hm.put(2,se2);
System.out.println(hm.get(1)。getSalt());



$ b


解决方案

您必须重写类的hashCode() equals()如果你打算在 HashMap 中使用 A 作为键,则c $ c> A 并且需要另一种形式的平等而不是参照平等。

自Java 7以来实现 hashcode()的小技巧:使用 Objects#hash (Object ...)

例如:

  public class A {
对象attr1,attr2,/ * ...,* / attrn;

@Override
public int hashcode(){
return Objects.hash(attr1,attr2,/ * ...,* / attrn);
}
}


Do I have to override hashCode() method, if I'm going to customize HashMap?

UPD: For example:

import java.util.HashMap;
import java.util.Objects;

/**
 *
 * @author dolgopolov.a
 */
public class SaltEntry {

    private long time;
    private String salt;

    /**
     * @return the time
     */
    public long getTime() {
        return time;
    }

    /**
     * @param time the time to set
     */
    public void setTime(long time) {
        this.time = time;
    }

    /**
     * @return the salt
     */
    public String getSalt() {
        return salt;
    }

    /**
     * @param salt the salt to set
     */
    public void setSalt(String salt) {
        this.salt = salt;
    }

    public boolean equals(SaltEntry o) {
        if (salt.equals(o.getSalt()) && time == o.getTime()) {
            return true;
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(String.valueOf(salt + time));
    }

    public static void main(String[] args) {
        SaltEntry se1 = new SaltEntry(), se2 = new SaltEntry();
        se1.setSalt("1");
        se2.setSalt("1");
        se1.setTime(1);
        se2.setTime(1);
        HashMap<String, SaltEntry> hm = new HashMap<String, SaltEntry>();
        hm.put("1", se1);
        hm.put("2", se2);
        System.out.println(hm.get("1").getSalt());

    }

}

解决方案

You have to override both hashCode() and equals() of the class A if you're going to use A as key in HashMap and need another form of equality than reference equality.

A little trick to implements hashcode() since Java 7: use Objects#hash(Object...).
For example:

public class A {
  Object attr1, attr2, /* ... , */ attrn ;

  @Override
  public int hashcode() {
    return Objects.hash(attr1, attr2, /* ... , */ attrn);
  }
}

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

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