GUID is a 128-bit long number, which is generally expressed in hexadecimal notation. The core idea of an algorithm is to combine the machine's Nic, local time, and a random number to generate a guid. Theoretically, if a machine generates 10000000 guids per second, it can ensure (in probability) that there will be no duplicates in 3240.
UUID is a new class in 1.5. in Java. util, it can generate a globally unique ID.
Import java. util. UUID;
Public class test {
Public static void main (string [] ARGs ){
UUID = UUID. randomuuid ();
System. Out. println (UUID );
}
}
Compile and run the output:
07ca3dec-b674-41d0-af9e-9c37583b08bb
Two methods are used to generate guid and UUID
Comm log library required
Import java.net. inetaddress;
Import java.net. unknownhostexception;
Import java. Security. messagedigest;
Import java. Security. nosuchalgorithmexception;
Import java. Security. securerandom;
Import java. util. Random;
Public class randomguid extends object {
Protected final org. Apache. commons. Logging. Log logger = org. Apache. commons. Logging. logfactory
. Getlog (getclass ());
Public String valuebeforemd5 = "";
Public String valueaftermd5 = "";
Private Static random myrand;
Private Static securerandom mysecurerand;
Private Static string s_id;
Private Static final int pad_below = 0x10;
Private Static final int two_bytes = 0xff;
Static {
Mysecurerand = new securerandom ();
Long secureinitializer = mysecurerand. nextlong ();
Myrand = new random (secureinitializer );
Try {
S_id = inetaddress. getlocalhost (). tostring ();
} Catch (unknownhostexception e ){
E. printstacktrace ();
}
}
Public randomguid (){
Getrandomguid (false );
}
Public randomguid (Boolean secure ){
Getrandomguid (secure );
}
Private void getrandomguid (Boolean secure ){
Messagedigest MD5 = NULL;
Stringbuffer sbvaluebeforemd5 = new stringbuffer (128 );
Try {
MD5 = messagedigest. getinstance ("MD5 ");
} Catch (nosuchalgorithmexception e ){
Logger. Error ("error:" + E );
}
Try {
Long time = system. currenttimemillis ();
Long Rand = 0;
If (secure ){
Rand = mysecurerand. nextlong ();
} Else {
Rand = myrand. nextlong ();
}
Sbvaluebeforemd5.append (s_id );
Sbvaluebeforemd5.append (":");
Sbvaluebeforemd5.append (long. tostring (time ));
Sbvaluebeforemd5.append (":");
Sbvaluebeforemd5.append (long. tostring (RAND ));
Valuebeforemd5 = sbvaluebeforemd5.tostring ();
Md5.update (valuebeforemd5.getbytes ());
Byte [] array = md5.digest ();
Stringbuffer sb = new stringbuffer (32 );
For (Int J = 0; j <array. length; ++ J ){
Int B = array [J] & two_bytes;
If (B <pad_below)
SB. append ('0 ');
SB. append (integer. tohexstring (B ));
}
Valueaftermd5 = sb. tostring ();
} Catch (exception e ){
Logger. Error ("error:" + E );
}
}
Public String tostring (){
String raw = valueaftermd5.touppercase ();
Stringbuffer sb = new stringbuffer (64 );
SB. append (raw. substring (0, 8 ));
SB. append ("-");
SB. append (raw. substring (8, 12 ));
SB. append ("-");
SB. append (raw. substring (12, 16 ));
SB. append ("-");
SB. append (raw. substring (16, 20 ));
SB. append ("-");
SB. append (raw. substring (20 ));
Return sb. tostring ();
}
// Demonstraton and self test of class
Public static void main (string ARGs []) {
For (INT I = 0; I <100; I ++ ){
Randomguid myguid = new randomguid ();
System. Out. println ("seeding string =" + myguid. valuebeforemd5 );
System. Out. println ("rawguid =" + myguid. valueaftermd5 );
System. Out. println ("randomguid =" + myguid. tostring ());
}
}
}
Similarly
UUID = UUID. randomuuid ();
System. Out. println ("{" + UUID. tostring () + "}");
UUID refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. Generally, the platform provides APIs for UUID generation. UUID is calculated according to the standards set by the Open Software Foundation (OSF), and uses Ethernet Card addresses, nanoseconds, chip ID codes, and many possible numbers. The combination of the following parts: the current date and time (the first part of UUID is related to the time. If you generate a uuid in seconds, the first part is different and the rest are the same), the clock sequence, and the globally unique IEEE machine identification number (if there is a nic, it is obtained from the NIC, and no Nic is obtained in other ways ), the unique defect of UUID is that the generated result string is long. The most common UUID standard is Microsoft's guid (globals unique identifiers ).
# Java Column