How to create a Java HashMap of user defined class type? Last Updated : 12 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Pre-requisite: Internal working of HashMap, HashMapIf we wish to create a HashMap of our own class, we need to ensure that the hashcode() of the key of HashMap doesn't change as if it happens then it is impossible to get the object value of the key from HashMap.On runtime, JVM processes hash code for each object and gives it on interest. When we alter an object's state, JVM calculates its hash code again which may result in memory leak. To make things work what we have to do is make sure that state change for a key object does not change the hash code of object i.e. the key must have properly overridden equals() and hashcode() methods for it to work correctly.One of the ways of doing this is by making key objects IMMUTABLE. Immutability allows you to get the same hashcode every time, for a key object. This is the primary motivation behind why Immutable classes like String, Integer or other wrapper classes are decent key object applicants. Learn more about this here- How to make class Immutable? Java // 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<Student, String> 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"); // prints Geeks1 System.out.println(map.get(one)); // prints Geeks2 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)); } } OutputGeeks1 Geeks2 Geeks1 Comment More infoAdvertise with us Next Article How to create a Java HashMap of user defined class type? S subhu1999 Follow Improve Article Tags : Java Practice Tags : Java Similar Reads How to Convert HashMap to ArrayList in Java? In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as:Extracting only the keys or values in the list form.Converting key-value pai 2 min read HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size()) HashMap is a data structure that uses a hash function to map identifying values, known as keys, to their associated values. It contains "key-value" pairs and allows retrieving value by key. The most impressive feature is it's fast lookup of elements especially for large no. of elements. It is not sy 6 min read Methods to Create Preallocated HashMap in Java 19 Java 19 has introduced some new methods to create preallocated HashMaps which can enhance the performance of your application. In this article, we will explore the concept of preallocated HashMaps and how to create them using the new methods in Java 19. HashMap is a widely used data structure in Jav 3 min read Create HashMap with Multiple Values Associated with the Same Key in Java In Java, HashMap is used to store the data in Key - Value pairs. One key object is associated with one value object. Below is the syntax for inserting the values into HashMap. HashMap<String, Integer> hm = new HashMap<String, Integer>(); hm.put("one", 100); hm.put("two", 200); hm.put("th 3 min read HashMap forEach(BiConsumer) method in Java with Examples The forEach(BiConsumer) method of HashMap class perform the BiConsumer operation on each entry of hashmap until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of the key-value pair of hashtable performed in the order of iteration. 3 min read Like