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()方法取到值。
如果有帮助,请帮忙点一下赞,谢谢 ^_^