I first learned about java-15.7 Map (5)-introduced the working principle of HashMap-Key changed. Can I get the original value? (Occasional interview questions)

Source: Internet
Author: User

I first learned about java-15.7 Map (5)-introduced the working principle of HashMap-Key changed. Can I get the original value? (Occasional interview questions)

In this chapter, we will discuss a special case where the Key has changed. Can we get the original value?

The answer is: Sometimes yes, sometimes no

1. Possible situations:

 

package com.ray.ch14;import java.util.HashMap;public class Test {public static void main(String[] args) {HashMap
 
   map = new HashMap
  
   ();Person person_1 = new Person();person_1.setHeight(180);person_1.setId(1);person_1.setName("person_1");Dog dog_1 = new Dog();dog_1.setId(1);dog_1.setName("dog_1");map.put(person_1, dog_1);person_1.setId(2);System.out.println(map.get(person_1));}}class Dog {private int id = 0;private String name = "";public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}class Person {private int id = 0;private String name = "";private int height = 0;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}}
  
 

Output:

 

Com. ray. ch14.Dog @ 1bc4459

 

We will not mention the above situation, because it is a conventional form. Below we will detail the circumstances that cannot be discussed.

 

2. No, it is the hashCode we overwrite, And the hashCode method is associated with an attribute (this is a common method, especially the id of the associated object)

 

package com.ray.ch14;import java.util.HashMap;public class Test {public static void main(String[] args) {HashMap
 
   map = new HashMap
  
   ();Person person_1 = new Person();person_1.setHeight(180);person_1.setId(1);person_1.setName("person_1");Dog dog_1 = new Dog();dog_1.setId(1);dog_1.setName("dog_1");map.put(person_1, dog_1);person_1.setId(2);System.out.println(map.get(person_1));}}class Dog {private int id = 0;private String name = "";public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}class Person {private int id = 0;private String name = "";private int height = 0;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}@Overridepublic int hashCode() {System.out.println("hashCode() invoke,hashcode:" + id + name.hashCode()+ height);return id + name.hashCode() + height;}}
  
 

Output:

 

HashCode () invoke, hashcode: 1443164103180
HashCode () invoke, hashcode: 2443164103180
Null

 

Explanation:

(1) We overwrite the original hashCode () method and associate it with all attributes.

(2) We changed the key in the main method, thus changing the hashCode Of the key. The output shows that the two keys are different, the first is the hashCode when we put the object, and the second is when we get it out later.

(3) The hashCode has changed, so the jvm thinks it can be changed. Even if the value stored in the map is still the original one, it cannot be obtained.

Conclusion: In this chapter, we use a special example to observe the working principle of Map again.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.