Unique id generated by JAVA UUID

Source: Internet
Author: User

Unique id generated by JAVA UUID
When designing a table, a requirement project must process more concurrent data. Similar orders cannot be duplicated and must be unique. I thought it would be nice to get a timestamp accurate to milliseconds. Later I thought it was wrong. There are many identical IDs in the test environment and they cannot be unique. The uuid jdk api says this: "indicates a class with a universal unique identifier (UUID. UUID indicates a 128-bit value ." In details, "UUID stands for the universal Unique Identifier (Universally Unique Identifier), which is a standard for Software construction and also known as the Open Software Foundation (OSF) is a part of the organization in the Distributed Computing Environment (Distributed Computing Environment, DCE) field. The purpose of UUID is to allow all elements in the distributed system to have unique identification information without specifying the identification information through the central control terminal. In this way, each user can create a UUID that does not conflict with others. In this case, duplicate names are not required during database creation. At present, the most widely used UUID is Microsoft's Globally Unique Identifiers (GUIDs, linux ext2/ext3 file system, LUKS encryption partition, GNOME, KDE, Mac OS X, and so on." UUID is composed of the following parts: (1) the current date and time. The first part of UUID is related to the time. If you generate a UUID, after several seconds, a UUID is generated, the first part is different, and the rest are the same. (2) Clock sequence (3) Globally Unique IEEE machine identification number. If there is a nic, it is obtained from the nic mac address, and no Nic is obtained in other ways. The code implementation is very convenient. You can directly call the UUID's randomUUID method to obtain the UUID object and then obtain the unique ID code. Public static void main (String [] args) {UUID uuid = UUID. randomUUID (); System. out. println (uuid);} RUN it. You can find that: 1 <font size = "4"> 65752c66-bd3f-4564-b8d6-92d66796e007 </font> is the unique identifier code. But it seems lengthy and unfriendly. It is more unfriendly to set parameters after a URL. It also takes more space to store a UUID. You don't have to worry too much about obtaining time. Obtain the eight-bit uuid id code, which is similar to the online code of Daniel and directly goes to the Code: <font size = "4"> public static String [] chars = new String [] {"", "B", "c", "d", "e", "f", "g", "h", "I", "j", "k ", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u ", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4 ", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E ", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O ", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y ", "Z"}; p Ublic static String getShortUuid () {StringBuffer stringBuffer = new StringBuffer (); String uuid = UUID. randomUUID (). toString (). replace ("-", ""); for (int I = 0; I <8; I ++) {String str = uuid. substring (I * 4, I * 4 + 4); int strInteger = Integer. parseInt (str, 16); stringBuffer. append (chars [strInteger % 0x3E]);} return stringBuffer. toString () ;}</font> with 300 tests, no problem. Enough to adapt to the environment scenario.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.