There is a UUID class in java.util, which is often used to generate irregular and unique serial numbers such as IDs or activation codes.
write a method to test
public static void main(String[] args) { String uuid = UUID.randomUUID().toString(); System.out.println(uuid); }
The result generated is: ae3c33ad-1f28-4a93-9320-c912f46171fc
length 36
In view of the fact that we generally use it as an ID, there is no middle "-", so we remove the middle "-"
public static void main(String[] args) { // String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString().replace("-", ""); System.out.println(uuid); }
The generated result is: 952d2bbafb9e408e8ffd13085150284a
length 32
Further encapsulated into UUID tool class UUIDUtil
public class UUIdUtil { public static String getUUID(){ return UUID.randomUUID().toString().replace("-", ""); } public static void main(String[] args) { System.out.println("UUID before format: " + UUID.randomUUID().toString()); System.out.println("Formatted UUID: " + getUUID()); } }What is finally generated is a hexadecimal unordered unique character sequence of length 32.