Java: Retrieving an element from a HashSet
You can HashMap<MyHashObject,MyHashObject>
instead of HashSet<MyHashObject>
.
Calling ContainsKey()
on your "reconstructed" MyHashObject
will first hashCode()
-check the collection, and if a duplicate hashcode is hit, finally equals()
-check your "reconstructed" against the original, at which you can retrieve the original using get()
This is O(1) but the downside is you will likely have to override both equals()
and hashCode()
methods.
It sounds like you're essentially trying to use the hash code as a key in a map (which is what HashSets do behind the scenes). You could just do it explicitly, by declaring HashMap<Integer, MyHashObject>
.
There is no get
for HashSets because typically the object you would supply to the get
method as a parameter is the same object you would get back.
If you know what element you want to retrieve, then you already have the element. The only question for a Set
to answer, given an element, is whether it contains()
it or not.
If you want to iterator over the elements, just use a Set.iterator()
.
It sounds like what you're trying to do is designate a canonical element for an equivalence class of elements. You can use a Map<MyObject,MyObject>
to do this. See this SO question or this one for a discussion.
If you are really determined to find an element that .equals()
your original element with the constraint that you MUST use the HashSet
, I think you're stuck with iterating over it and checking equals()
yourself. The API doesn't let you grab something by its hash code. So you could do:
MyObject findIfPresent(MyObject source, HashSet<MyObject> set)
{
if (set.contains(source)) {
for (MyObject obj : set) {
if (obj.equals(source))
return obj;
}
}
return null;
}
Brute force and O(n) ugly, but if that's what you need to do...
If you know the order of elements in your Set, you can retrieve them by converting the Set to an Array. Something like this:
Set mySet = MyStorageObject.getMyStringSet();
Object[] myArr = mySet.toArray();
String value1 = myArr[0].toString();
String value2 = myArr[1].toString();