Hey. I am wondering if it is possible to directly access the contents of the HashSet if you have a Hashcode for the object you are looking for, sort of using HashCode as the key in the HashMap.
I assume this might work something like this:
MyObject object1 = new MyObject(1); Set<MyObject> MyHashSet = new HashSet<MyObject>(); MyHashSet.add(object1) int hash = object1.getHashCode MyObject object2 = MyHashSet[hash]???
Thanks!
edit: Thanks for the answers. Well, I understand that I could push the HashSet contract a bit, but for this particular project, equality is determined solely by the hash code, and I know for sure that there will only be one object per hash code / hashbucket. The reason I was rather reluctant to use HashMap is because I will need to convert the primitive ints to which I map Integer objects, since the HashMap only accepts objects as keys, and I am also worried that this may affect performance. Is there anything else I could do to implement something like this?
java hashcode hashset hash
Kode47
source share