Xfce4 :
Assume myHashSet = HashSet<SomeClass>
where SomeClass.hashcode () = someField.hashcode()
How can I return an element with the specified hashcode, i.e:
myHashSet.getElementWithHashCode((other as SomeClass).someField.hashcode())
The element inside the HashSet
and other
are different objects with different property values except someField
value.
It is weird that there is no such function in HashSet. No one needed that before? What is the quickest way around?
Malte Hartwig :
I don't know whether this could you in your case, it would depends on whether it used hashCode
or equals
internally. Some sources online that have a similar problem are looking for an equals-based solution.
Anyway, you can use built-in functions like find
or first
to implement it yourself:
fun <E> HashSet<E>.findByHashCode(other: E): E? = firstOrNull { it.hashCode() == other.hashCode() }