I need to create a unique id
for each Person
object.
public interface Person
{
String getName();
}
public class Chef implements Person
{
String name;
....
// all other instance variables are not unique to this object.
}
public class Waiter implements Person
{
String name;
....
// all other instance variables are not unique to this object.
}
All other instance variables inside the Chef
are not unique to a particular Chef
. Nor can we add any extra variables inside the Chef
class to make it unique. This is because this information is coming from a back-end server and I cannot modify the Chef
class. This is a distributed system.
I want to create an integer that lets me map this Person
object. I have tried to create a "unique" id
.
private int makeId(Person person)
{
int id = person.getName()
.concat(person.getClass().getSimpleName())
.hashCode();
return id;
}
However, I know that this is not really unique because hashCode for a name is not guaranteeing anything uniqueness.
Without using random can I make this id
unique?
I apologize for the misunderstanding, but I cannot add more fields to my Chef
or Waiter
object class and the application is distributed.
What about adding a globally unique identifier (GUID)?
A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.
In Java, it is called a UUID. For example:
UUID uuid = java.util.UUID.randomUUID();
System.out.println(uuid.toString());