JAVA UUID generates a unique identity

Source: Internet
Author: User

writer:bysocket (mud and brick pulp carpenter)

    • Micro-Blog: Bysocket
    • Watercress: Bysocket

reprint it anywhere u want

Demand

When designing a table, the project should deal with some data that is more concurrent, such that the order number cannot be duplicated, so it must remain unique. Originally thought to have a time stamp, accurate to the millisecond should be good. Later thought is wrong, the test environment a lot of the same ID, can not reach the unique identity.

Uuid

This is what the JDK API says:
"Class that represents a universally unique identifier (UUID). The UUID represents a 128-bit value. "

in detail, it is:
"UUID meaning is a universal unique identification code (universally unique Identifier), which is a software construction standard and is also an organization of the Open Software Foundation, OSF, in a distributed computing environment ( Distributed Computing Environment, DCE) part of the field. The purpose of the UUID is to allow all elements in a distributed system to have unique identification information without the need to specify the information through the central control terminal. In this way, everyone can create a UUID that does not clash with others. In such a case, there is no need to consider the name duplication problem when the database is established. Currently the most widely used UUID is Microsoft's globally Unique Identifiers (GUIDs), while other important applications are the Linux ext2/ext3 file system, LUKS encryption partition, GNOME, KDE, Mac OS X, and so on. "

The UUID is composed of the following parts:

(1) The current date and time, the first part of the UUID is related to the time, if you generate a UUID after a few seconds to generate a UUID, then the first part is different, the rest is the same.

(2) clock sequence

(3) Globally unique IEEE machine identification number, if there is a network card, from the network card MAC address obtained, no network card is obtained in other ways.

 

Code implementation

conveniently, the UUID object can be obtained by invoking the Randomuuid method of the UUID directly, and then the unique identifier is obtained.

?
1 2 3) 4 5 public static void Main (string[] args) {uuid uuid = Uuid.randomuuid (); SYSTEM.OUT.PRINTLN (UUID); }

run a bit and you can find it from the console:

?
1 <fontsize= "4" > 65752c66-bd3f-4564-b8d6-92d66796e007</font>

This is the only sign code. But it looks long and not friendly. If you make arguments after the URL, it's not as friendly. It also takes more space to store a UUID. You don't have to think too much about getting the time.

Get the eight-bit UUID ID code

imitate the online bull code, directly on the code:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <fontsize= "4" >public static string[] chars = new string[]         {              "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 ", &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB sp;      "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",       & nbsp;      "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 "        };      &NBSP;&NB Sp;  public 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 environmental scene.

writer:bysocket (mud and brick pulp carpenter)

    • Micro-Blog: Bysocket
    • Watercress: Bysocket

reprint it anywhere u want

JAVA UUID generates a unique identity

Related Article

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.