I am looking for information about HashSet Design Managers. As far as I know, my question applies to both Java and C # HashSets, making me think that there must be some good reason for this, although I canβt think of myself.
After I inserted an element into a HashSet, why is it impossible to get this element without an enumeration, is it hardly an effective operation? Moreover, the HashSet is clearly built in a way that supports efficient extraction.
It would be useful for me to have Remove (x) and Contains (x) return the actual element that is being removed or contained. This is not necessarily the element that I pass to the Remove (x) or Contains (x) function. Of course, I think that I could achieve the same effect through HashMap, but why spend all this space and effort when it is quite possible to do it with a set?
I can appreciate that there may be some design issues, that adding this feature will allow you to use a HashSet that is not consistent with their role or future role in the structure, but if so, what are these design issues?
Edit
To answer a few more questions, more details:
I use an immutable reference type with overridden hashcode, equals, etc. to emulate the value type in C #. Let say that the type has members A, B and C. Hashcode, equals, etc. It depends only on A and B. Given that some A and BI want to get this equivalent element from hashset and get it C. I won 'I can use a HashSet for this, but I would like to know if there are good reasons for this. Following is the pseudo code:
public sealed class X{ object A; object B; object extra; public int HashCode(){ return A.hashCode() + B.hashCode(); } public bool Equals(X obj){ return obj.A == A && obj.B == B; } } hashset.insert(new X(1,2, extra1)); hashset.contains(new X(1,2));
java c # hashset
sooniln Sep 29 '09 at 20:41 2009-09-29 20:41
source share