简体   繁体   English

从java.util.Map获取值

[英]Getting values from java.util.Map

I have Map<Date, String . 我有Map<Date, String I have two Date objects a, b that are equal. 我有两个相同的Date对象a, b I put a string value to the map associated with key a . 我将一个字符串值放到与键a关联的地图上。 Then I try to get map value associated with keys a and b but only a returns value that I've put. 然后我尝试获取与键ab相关联的映射值,但只返回我放置a返回值。 Is it possible to get my value with b key. 是否可以用b键获取我的价值。 I know this is possible when keys are simple strings. 我知道当键是简单的字符串时这是可能的。 Why this doesn't work with other type of objects? 为什么这不适用于其他类型的对象?

public class Main {

public static void main(String[] args) {
    Map<Date, String> map = new HashMap<Date, String>();

    Date a = new Date(20131105);
    Date b = new Date(20131105);

    map.put(a, "sweet");

    System.out.println(map.get(a));
    System.out.println(map.get(b));
}

static class Date {
    private int ymd;

    public Date(int ymd) {
        this.ymd = ymd;
    }

    public int getYmd() {
        return ymd;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Date) {
            return ((Date) obj).ymd == ymd;
        }
        return false;
    }
}

} }

The output is: 输出是:

sweet
null

Since you're using an HashMap for storing your date objects, you have to override the hashCode() method because the key objects are stored in the data structure using their hashCode. 由于您使用HashMap存储日期对象,因此必须覆盖hashCode()方法,因为密钥对象使用其hashCode存储在数据结构中。

Very basic hashCode() overriden (just for illustration): 非常基本的hashCode()覆盖(仅用于说明):

@Override
public int hashCode(){
   return ymd;
}

Output : 输出:

sweet
sweet

A hashmap hashes the elements using their hashCode function. hashmap使用hashCode函数对元素进行哈希处理。 For most types the hash code is computed using the reference of the object and this is the case with Date. 对于大多数类型,哈希代码是使用对象的引用计算的,这是Date的情况。 While in your case the two dates have same value, they are not the same object. 虽然在您的情况下,两个日期具有相同的值,但它们不是同一个对象。 Thus they have different references and so their hash codes differ. 因此它们具有不同的引用,因此它们的哈希码不同。 When you lookup an element in a HashMap its hashCode is used and so as b 's hashCode is different from a 's hashCode you can not find an element with key a by using b . 当您在寻找一个元素HashMap使用其的hashCode等为bhashCode是从不同的ahashCode你不能找到键的元素a使用b

You need to implement hashCode() method in your Date class 您需要在Date类中实现hashCode()方法

Add below code in your Date class 在Date类中添加以下代码

    @Override
    public int hashCode() {              
          return ymd;
    }

output 产量

sweet
sweet

Because String,Integer and all wrapper class override hasCode and equals both the method. 因为String,Integer和所有包装类都覆盖hasCode并且等于该方法。 But In your case equals() method will return true but you have not override hasCode() method so it will generate different hashcode for both a and b .So map.get(b) will return null value. 但在你的情况下,equals()方法将返回true,但你没有覆盖hasCode()方法,因此它将为a和b生成不同的哈希码。所以map.get(b)将返回null值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM