Hashcode and Equals in Hashmap

Hi Folks,

I am trying to debug a sample code to get key Value pairs from Hashmap. As I wanted to avoid duplicates I have implemented Hashcode() and equals() methods. But the duplicates are still there. Also when I am trying to debug the class Dog, the step wise debugging is not going to the equals() and Hashcode() methods but skipping out of the dog class after the constructor.

Dog Class
class Dog {
	String color;
	Dog(String c){
	this.color = c;
	}------------The step wise debugging skipping after this line and going into the main method class back.
	public boolean equals(Object o){
		return ((Dog) o).color.equals(this.color);
	public int hashcode(){
		return color.length();
	public String toString(){
		return color + " dog";

Open in new window

Main Method Class
public class TestHashMap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<Dog, Integer> hashMap = new HashMap<Dog, Integer>();
		  Dog d1 = new Dog("Black");
		  Dog d2 = new Dog("Green");
		  Dog d3 = new Dog("Yellow");
		  Dog d4 = new Dog("Yellow");
		  hashMap.put(d3, 40);
		  hashMap.put(d4, 25);
		System.out.println("Number of objects in hasMap " +hashMap);
		for(Entry<Dog, Integer> entry : hashMap.entrySet()){
			System.out.println(entry.getKey().toString() + " - " +entry.getValue());



Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
Try capitalizing the "C" in "hashCode." This is a place where the @Override annotation would have saved you; the compiler would have reported that your hashcode() method didn't actually override anything.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Of course that answer has nailed the problem. It has to be said that your hashing approach is not good though. Why? Clue: imagine a hash table containing Dog instances with colours "blue" and "grey"
zzynxSr. Software engineerCommented:
...and since the String class also implements hashCode() don't reinvent the wheel and use it:
return color.hashCode();

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

I was rather hoping kalyangkm would come up with that ;)
kalyangkmAuthor Commented:
Thanks Guys.
kalyangkmAuthor Commented:
CEHJ, I am trying to explore Hashmaps and its in this process I have to do this.
zzynxSr. Software engineerCommented:
Thanx 4 axxepting.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.