📜  如何创建用户定义类类型的Java HashMap?

📅  最后修改于: 2022-05-13 01:55:36.771000             🧑  作者: Mango

如何创建用户定义类类型的Java HashMap?

先决条件: HashMap、HashMap的内部工作
如果我们希望创建我们自己的类的 HashMap,我们需要确保HashMap 的键的 hashcode() 不会发生变化,就好像它发生了一样,那么就不可能从 HashMap 中获取键的对象值。

在运行时,JVM 处理每个对象的哈希码并按兴趣提供。当我们改变一个对象的状态时,JVM 会再次计算它的哈希码,这可能会导致内存泄漏。为了使事情正常工作,我们必须确保键对象的状态更改不会更改对象的哈希码,即键必须具有正确覆盖的 equals() 和 hashcode() 方法才能正常工作。

这样做的一种方法是使关键对象IMMUTABLE 。不变性允许您每次都为关键对象获取相同的哈希码。这就是为什么像 String、Integer 或其他包装类这样的不可变类是一个不错的关键对象申请者的主要动机。在此处了解更多信息 - 如何使类不可变?

// Java example to create a Java HashMap
// of user-defined class type
  
import java.util.*;
import java.io.*;
  
// User defined class
public class CustomKeyObject {
  
    public static class Student {
  
        private int rollno;
        private String name;
  
        // Constructor
        public Student(int rollno, String name)
        {
            this.rollno = rollno;
            this.name = name;
        }
  
        public String getname()
        {
            return this.name;
        }
  
        public int getmarks()
        {
            return this.rollno;
        }
  
        public void setname(String name)
        {
            this.name = name;
        }
  
        public void setmarks(int rollno)
        {
            this.rollno = rollno;
        }
  
        // Overriding the hashcode() function
        @Override
        public int hashCode()
        {
  
            // uses roll no to verify the uniqueness
            // of the object of Student class
  
            final int temp = 14;
            int ans = 1;
            ans = temp * ans + rollno;
            return ans;
        }
  
        // Equal objects must produce the same
        // hash code as long as they are equal
        @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Student other = (Student)o;
            if (this.rollno != other.rollno) {
                return false;
            }
            return true;
        }
    }
  
    // main method
    public static void main(String[] args)
        throws NumberFormatException,
               IOException
    {
        HashMap map = new HashMap<>();
  
        Student one = new Student(1, "Geeks1"); // key1
  
        Student two = new Student(2, "Geeks2"); // key2
  
        // put keys in map
        map.put(one, one.getname());
        map.put(two, two.getname());
  
        // changing key state so that
        // hashcode() should be calculated again
        one.setname("Not Geeks1");
        two.setname("Not Geeks2");
  
        // still prints Geeks1
        System.out.println(map.get(one));
  
        // still prints Geeks1
        System.out.println(map.get(two));
  
        // try with newly created key with same Rollno
        Student three = new Student(1, "Geeks3");
  
        // we get Geeks1
        System.out.println(map.get(three));
    }
  
    // This code is contributed by Subhesh
}
输出:
Geeks1
Geeks2
Geeks1