How do I create a unique ID in Java?
Solution 1
Create a UUID.
String uniqueID = UUID.randomUUID().toString();
Solution 2
If you want short, human-readable IDs and only need them to be unique per JVM run:
private static long idCounter = 0;
public static synchronized String createID()
{
return String.valueOf(idCounter++);
}
Edit: Alternative suggested in the comments - this relies on under-the-hood "magic" for thread safety, but is more scalable and just as safe:
private static AtomicLong idCounter = new AtomicLong();
public static String createID()
{
return String.valueOf(idCounter.getAndIncrement());
}
Solution 3
java.util.UUID
: toString() method
Solution 4
Here's my two cent's worth: I've previously implemented an IdFactory
class that created IDs in the format [host name]-[application start time]-[current time]-[discriminator]. This largely guaranteed that IDs were unique across JVM instances whilst keeping the IDs readable (albeit quite long). Here's the code in case it's of any use:
public class IdFactoryImpl implements IdFactory {
private final String hostName;
private final long creationTimeMillis;
private long lastTimeMillis;
private long discriminator;
public IdFactoryImpl() throws UnknownHostException {
this.hostName = InetAddress.getLocalHost().getHostAddress();
this.creationTimeMillis = System.currentTimeMillis();
this.lastTimeMillis = creationTimeMillis;
}
public synchronized Serializable createId() {
String id;
long now = System.currentTimeMillis();
if (now == lastTimeMillis) {
++discriminator;
} else {
discriminator = 0;
}
// creationTimeMillis used to prevent multiple instances of the JVM
// running on the same host returning clashing IDs.
// The only way a clash could occur is if the applications started at
// exactly the same time.
id = String.format("%s-%d-%d-%d", hostName, creationTimeMillis, now, discriminator);
lastTimeMillis = now;
return id;
}
public static void main(String[] args) throws UnknownHostException {
IdFactory fact = new IdFactoryImpl();
for (int i=0; i<1000; ++i) {
System.err.println(fact.createId());
}
}
}
Solution 5
Generate Unique ID Using Java
UUID is the fastest and easiest way to generate unique ID in Java.
import java.util.UUID;
public class UniqueIDTest {
public static void main(String[] args) {
UUID uniqueKey = UUID.randomUUID();
System.out.println (uniqueKey);
}
}
Related videos on Youtube
Comments
-
Supertux about 3 years
I'm looking for the best way to create a unique ID as a String in Java.
Any guidance appreciated, thanks.
I should mention I'm using Java 5.
-
Developer Marius Žilėnas about 6 yearsTake a look at this stackoverflow.com/a/9176060/1737819 .
-
-
pjp over 14 yearsIt's not all that human readable tho...
-
Joachim Sauer over 14 years@pjp: a truely randomly generated ID usually isn't human-readable. And making it human-readable usually makes it longer which in turn makes it less human-readable.
-
Adamski over 14 yearsI prefer Michael's method to the UUID approach as sequential IDs are more typically useful / easier when debugging. Also, UUID.randomUUID() isn't 100% guaranteed to return a unique value.
-
aperkins over 14 yearsI wasn't aware he wanted a human readable unique ID ... that would increase the difficulty quite a bit
-
Joachim Sauer over 14 years@Adamski: this method will only create unique values as long as you don't restart the JVM, have only 1 JVM running and as long as the counter doesn't overflow. All those assumptions can easily be broken. UUID.randomUUID() is actually more reliable in those situations.
-
aperkins over 14 yearsAnother note - if you only need application uniqueness, and AtomicInteger (as noted by Michael Borgwardt) is a much better option, but if you need global uniqueness, a UUID is a much better bet.
-
aperkins over 14 yearsWhile it is not 100% guaranteed, the odds are so low of you colliding with anyone (as the entire space is larger than the number of atoms estimated to exist in the Universe) as to be 100% guaranteed. And if you need global uniqueness, it is the easiest way to achieve that. However, if you only need local uniqueness (i.e. to a current existing application) then AtomicInteger is definitely the way to go.
-
Farhad over 7 yearsWhy can't we use
System.currentTimeMillis
to generate a monotonically increasing sequence and add some random positive integer salt? -
aperkins over 7 yearsYou certainly can @Farhad, but the UUID is about the only way to guarantee uniqueness across multiple servers with large traffic volume.
-
Mercury about 7 yearsI liked the additional currentTimeMillis which really makes this random.
-
Pavel Nguyen almost 6 yearsUUID.randomUUID() already generates ids using "cryptographically strong" random number generator. What does it mean to add 'a bit more randomness'? What do you get by attaching a random 'salt' based on epoch time?
-
Paramvir Singh Karwal about 5 yearsif I remember correctly
UUID.randomUUID()
already uses time factor while creating random ID. -
FishingIsLife about 2 yearsIn addition to the current millis suggestion. If your application runs on different server or moves the server, the system clock might differ. So ids based on the system clock might become duplicates