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(d1,10);
		  hashMap.put(d2,20);
		  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

kalyangkmAsked:
Who is Participating?
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.
0

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
CEHJCommented:
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"
0
zzynxSoftware engineerCommented:
...and since the String class also implements hashCode() don't reinvent the wheel and use it:
return color.hashCode();

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

CEHJCommented:
I was rather hoping kalyangkm would come up with that ;)
0
kalyangkmAuthor Commented:
Thanks Guys.
0
kalyangkmAuthor Commented:
CEHJ, I am trying to explore Hashmaps and its in this process I have to do this.
0
zzynxSoftware engineerCommented:
Thanx 4 axxepting.
Success!
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.