I recognise that sounds a bit mad but to explain what I mean:
I have a Collection
(eg HashSet) containing several quite slow initialisation objects and I want to see if the Collection
already contains a particular object. Let's use Vector3d
as an example (I know that is not expensive to initialise).
So the Collection
contains:
Vector3d(1,1,1)
Vector3d(2,1,1)
Vector3d(3,1,1)
And I want to ask the Collection
the question "does the Collection
contain a Vector3d
with x=2
, y=1
and z=1
(i.e. I already know the data the .contains()
method would hash against). So I could create a new Vector3d(2,1,1)
and then use .contains()
on that but as I said the objects initialisation is slow, or I could run through the entire Collection
manually checking (which is what I'm doing now) but thats (as I understand it) slower than .contains()
since it doesn't use hash. Is there a better way to do this?
The objects in question are mutable but the data that the equals
method is based upon is not. (In my case they are blocks at x,y,z co-ordinates, the contents of the blocks may change but the x,y,z co-ordinates will not)