HashMap.get(Object)可以正常取到值吗?

HashMap主要是通过hashCode()方法来对引用进行辨识的,
只要Object.hashCode()的值相同,
就可以正常从HashMap中通过get()方法取到值。

源码解析

HashMap.get()方法 :

在这里插入图片描述

hash()方法:

在这里插入图片描述

结论:

从源码中可以看出,HashMap主要是通过hashCode()方法来对引用进行辨识的,只要Object.hashCode()的值相同,就可以正常从HashMap中通过get()方法取到值。

举例一:HashMap.get(LocalDateTIme)

测试代码:

public static void main(String[] args) {
    LocalDateTime dateTime1 = LocalDateTime.of(2020, 4, 30, 0, 0, 0);
    LocalDateTime dateTime2 = LocalDateTime.of(2020, 4, 30, 0, 0, 0);
    LocalDateTime dateTime3 = LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.of(0, 0, 0));
    LocalDateTime dateTime4 = LocalDateTime.parse("2020-04-30 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    LocalDateTime dateTime5 = LocalDateTime.now();
    Map<LocalDateTime, Double> localDateTimeMap = new HashMap<>();
    localDateTimeMap.put(dateTime1, 1.0);
    String format = "result%s: address: %s, hashcode: %s, key: %s\n";
    System.out.printf(format, "1", System.identityHashCode(dateTime2), dateTime2.hashCode(), localDateTimeMap.getOrDefault(dateTime2, 2.0));
    System.out.printf(format, "2", System.identityHashCode(dateTime3), dateTime3.hashCode(), localDateTimeMap.getOrDefault(dateTime3, 2.0));
    System.out.printf(format, "3", System.identityHashCode(dateTime4), dateTime4.hashCode(), localDateTimeMap.getOrDefault(dateTime4, 2.0));
    System.out.printf(format, "4", System.identityHashCode(dateTime5), dateTime5.hashCode(), localDateTimeMap.getOrDefault(dateTime5, 2.0));
}

输出结果:

result1: address: 1355316001, hashcode: 4137246, key: 1.0
result2: address: 1597462040, hashcode: 4137246, key: 1.0
result3: address: 403716510, hashcode: 4137246, key: 1.0
result4: address: 853119666, hashcode: 207251743, key: 2.0

结论:

​ LocalDateTime重写了hashCode()方法,所以在Map中可以放心使用LocalDateTime类型作为Key。

​ 只要Object.hashCode()的值相同,就可以正常从HashMap中通过get()方法取到值。

举例二:HashMap.get(String)

测试代码:

public static void main(String[] args) {
    String string1 = "You are such a good person.";
    String string2 = "You are such a good person.";
    String string3 = new StringBuffer("You are such a good person.").toString();
    String string4 = String.format("You are such a %s person.", "good");
    String string5 = "";
    Map<String, Double> stringMap = new HashMap<>();
    stringMap.put(string1, 1.0);
    String format = "result%s: address: %s, hashcode: %s, key: %s\n";
    System.out.printf(format, "1", System.identityHashCode(string2), string2.hashCode(), stringMap.getOrDefault(string2, 2.0));
    System.out.printf(format, "2", System.identityHashCode(string3), string3.hashCode(), stringMap.getOrDefault(string3, 2.0));
    System.out.printf(format, "3", System.identityHashCode(string4), string4.hashCode(), stringMap.getOrDefault(string4, 2.0));
    System.out.printf(format, "4", System.identityHashCode(string5), string5.hashCode(), stringMap.getOrDefault( string5, 2.0));
}

输出结果:

result1: address: 93122545, hashcode: -1752737151, key: 1.0
result2: address: 2083562754, hashcode: -1752737151, key: 1.0
result3: address: 1239731077, hashcode: -1752737151, key: 1.0
result4: address: 557041912, hashcode: 0, key: 2.0

结论:

​ String重写了hashCode()方法,所以在Map中可以放心使用String类型作为Key。

​ 只要Object.hashCode()的值相同,就可以正常从HashMap中通过get()方法取到值。




如果有帮助,请帮忙点一下赞,谢谢 ^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值