How to Generate an unique ID in Java

Example

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);  
}  
}  

Result


0a6a3de7-b974-42d0-af9e-9c23473b09b9

10 comments on “How to Generate an unique ID in Java

  1. Is there any way to get a unique id that will be the same every time for a particular machine regardless of OS?

    Reply
  2. Hi, what if we want to generate unique ID with fixed length then how the code looks like.

    Reply
  3. I am trying to generate a unique 10 digit alpha numeric number, any way to do that in a distributed system

    Reply
  4. Its nice. Could have explored the options supported in UUID and the limitations identified to explore more.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *