There are three way to generate unique id in java.
1) the UUID class provides a simple means for generating unique ids.
UUID id = UUID.randomUUID();
System.out.println(id);
2) SecureRandom and MessageDigest
//initialization of the application
SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
//generate a random number
String randomNum = new Integer(prng.nextInt()).toString();
//get its digest
MessageDigest sha = MessageDigest.getInstance("SHA-1");
byte[] result = sha.digest(randomNum.getBytes());
System.out.println("Random number: " + randomNum);
System.out.println("Message digest: " + new String(result));
3) using a java.rmi.server.UID
UID userId = new UID();
System.out.println("userId: " + userId);