---
title: "Discussion on \"Reason To Override equals() & hashCode()\" | Hashnode"
description: "Discussion on \"Reason To Override equals() & hashCode()\". In this blog, we will see why we generally override java equals and hashCode methods. Besides that, we will see why we should override both of them."
url: "https://hashnode.com/posts/reason-to-override-equals-hashcode/6305bd13750cde48abba9f96"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Reason To Override equals() & hashCode()](https://harshmalik.hashnode.dev/reason-to-override-equals-hashcode?utm_source=hashnode&utm_medium=feed)

In this blog, we will see why we generally override java equals and hashCode methods. Besides that, we will see why we should override both of them.

Aug 24, 2022 · 4 min read · harshmalik.hashnode.dev  
Read the article

-   [#java](https://hashnode.com/tag/java)
-   [#developer](https://hashnode.com/tag/developer)
-   [#software-development](https://hashnode.com/tag/software-development)
-   [#software-engineering](https://hashnode.com/tag/software-engineering)
-   [#technical-writing-1](https://hashnode.com/tag/technical-writing-1)

## Written by

[Harsh Malik](https://hashnode.com/@MalikHarsh)

@MalikHarsh · Noida, IN

  
[View profile](https://hashnode.com/@MalikHarsh)

Software Engineer

I am a Software engineer. I love to learn and write my learnings

## Responses 1

Markdown supported · ⌘↵ to post

[Deepak A L](https://hashnode.com/@DeepakLalchandani)

[Aug 24, 2022](https://hashnode.com/posts/reason-to-override-equals-hashcode/6305bd13750cde48abba9f96/comment/63062072d69cbd567158787e)

public class codeTest { public static void main(String\[\] args) { Bank b1 = new Bank("Harsh", 999); Bank b2 = new Bank("Arjun", 999); Bank b3 = new Bank("Arjun", 1199); Bank b4 = new Bank("Harsh", 999); HashMap<Bank,String> map = new HashMap<>(); map.put(b1 , "Delhi"); map.put(b2 , "Mumbai"); map.put(b3,"Hyderabad"); System.out.println(map.get(b4)); //Here you would expect the answer should be Delhi, but it would be null. } } class Bank { String customerName; int accountBalance; public Bank(String name, int balance) { customerName = name; accountBalance = balance; } @Override public boolean equals(Object o) { //some implementation of equals method } }

Can you post the complete code here which is working
