I'm trying to find the fastest way to find a key in a HashMap
and return it.
I tried using containsKey
but I'm initializing the object before this so the hashcode is different and it can't find it.
I used linear search like this:
Box b = null;
Box box = new Box(10, 5);
for (Box e : cells.keySet()) {
if (box.equals(e)) {
contains = true;
b = e;
}
}
Box
is a class that contains x
and y
fields. It works fine but I was wondering if there is a faster way to do it.