Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Tim Cooke
Devaka Cooray
Sheriffs:
Liutauras Vilda
paul wheaton
Rob Spoor
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Piet Souris
Mikalai Zaikin
Bartenders:
Carey Brown
Roland Mueller
Forum:
Programmer Certification (OCPJP)
Hash Map question ...
Brian Lugo
Ranch Hand
Posts: 165
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a question relating to the uniqueness of keys in HashMap. I wrote this program and apparently the HashMap seems to be accepting duplicate keys. Could some one please point out what I am doing wrong or if I am missing something?
import java.util.*; class MyMapKeyType implements Comparable { private int i; public MyMapKeyType(int n) { i = n; } public boolean equals(Object o) { return (o instanceof MyType) && (i == ((MyMapKeyType)o).i); } public int hashCode() { return i; } public String toString() { return i + " "; } public int compareTo(Object o) { int i2 = ((MyMapKeyType)o).i; return (i2 < i ? -1 : (i2 == i ? 0 : 1)); } } public class TestHashMap { public static void main(String[] args) { Map hm = new HashMap(); MyMapKeyType a1 = new MyMapKeyType(1); hm.put(a1, new Integer(7)); MyMapKeyType a2 = new MyMapKeyType(1); hm.put(a2, new Integer(7)); hm.put(a1, new Integer(7)); System.out.println(hm); hm.put("abc", new Integer(2)); hm.put("abc", new Integer(2)); System.out.println(hm); } }
Thanks,
Brian
Mapraputa Is
Leverager of our synergies
Posts: 10065
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Are you sure your equals method should contain "(o instanceof MyType)", not "(o instanceof MyMapKeyType)" ?
-------------
Mapraputa Is,
aka hashMap
Brian Lugo
Ranch Hand
Posts: 165
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Mapraputa !
Yep that was it. How foolish of me
...
Why does'nt the compiler correct my foolish mistakes ?
No - Just kidding ...
Brian
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Accessing HashMaps
Problem in Creating HashMap object then display in sorted and unsorted order
what It means if you don't override equals()
HashMap question
Overriding equals() and hashCode()
More...