I want to generate UUIDs based on objects. Objects that are equal need to have the same UUID.
I read about the type 3 UUIDs whose value is based on a name and namespace. java.util.UUID
has a nameUUIDFromBytes
method that takes a byte array as argument.
So I was thinking of serializing my objects into byte arrays and feeding those to the nameUUIDFromBytes
method.
But I am confused about that namespace aspect of the UUID. Does that mean the UUID will be different when generated on another machine?
What is the best way to generate UUIDs such that when obj1.equals(obj2) == true
, then uuid1.equals(uuid2) == true
even when uuid1 is generated on another machine than uuid2?
EDIT: to those who voted this as a duplicate of how to implement a hashCode, please reopen this. I am asking about UUIDs, not hashCodes. Unless you think the best way to generate UUIDs is to use a hashCode. If so, please write an answer as to why that is the best way instead of closing this question with something that hardly has anything to do with it.