Java Development _ UUID (universally unique identifier, globally unique identifier) and guid (globally unique identifier, globally unique identifier)

Source: Internet
Author: User

Guid: that is, globally unique identifier (globally unique identifier) is also called UUID (universally unique identifier ).

Therefore, the guid is the UUID.

GUID is a 128-bit long number, which is generally expressed in hexadecimal notation.AlgorithmThe core idea 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.

The following is a demo:

PackageCom. mytest;ImportJava. util. UUID;/*** Jdk1.5 or later **/Public ClassUuidtest {Public Static VoidMain (string [] ARGs) {UUID=UUID. randomuuid (); system. Out. println (UUID );}}

Output result:

 
1Fd61384b-05a5-4030-885e-a6bd3b60ef1a

The following is how to remove "-" From UUID "-"

 

 1   /**  2   *  3    */  4   Package  Com. b510;  5   6   Import  Java. util. UUID;  7   8   /** 9   * @ Date 2012-11-7  10   *  @ Author  Xhw  11   *  12    */  13   Public   Class  Uuidtest {  14       /**  15  * Obtain a uuid that removes the "-" symbol.  16   *  17   *  @ Return  String UUID  18        */  19       Public   Static  String getuuid (){  20 String S = UUID. randomuuid (). tostring ();  21          //  Remove the "-" symbol  22           Return S. substring (0, 8) + S. substring (9, 13) + S. substring (14, 18) + S. substring (19, 23) + S. substring (24 );  23   }  24   25       /**  26   * Obtain a uuid that removes the "-" symbol.  27   *  28  *  @ Return  29        */  30       Public   Static  String getuuid (){  31 String S = UUID. randomuuid (). tostring ();  32           //  Remove the "-" symbol  33           Return S. Replace ("-","" ); 34   }  35   36       /**  37   * Obtain the specified UUID.  38   *  39   *  @ Param  Number  40   * Number of UUID to be obtained for int  41   * @ Return  String [] UUID Array  42        */  43       Public   Static String [] getuuid ( Int  Number ){  44           If (Number <1 ){  45               Return   Null  ; 46   }  47 String [] Ss = New  String [number];  48           For ( Int I = 0; I <number; I ++ ){  49               If (I % 2 = 0 ){  50 Ss [I] = Getuuid ();  51 } Else  {  52 Ss [I] = Getuuid ();  53   }  54   }  55           Return  SS;  56   }  57   58       Public  Static   Void  Main (string [] ARGs ){  59 String [] Ss = getuuid (10 );  60           For ( Int I = 0; I <ss. length; I ++ ){  61   System. Out. println (ss [I]);  62   }  63   } 64   65 }

Output result:

 1   /**  2   Ca31b9c6bff04ecfb5dfd1675905338d  3   78af094061c04fc6b578a0f62aa09373  4   51f228083c6e4dcb855a8e733efded7c  5   Fceca14fdc194d889c05c1dcf8ccaf3b  6   4c7695ed08494b978fa9737dfe36f6a5 7   B7573dbadfe84ba2b3659d1e49f8bf08  8   F4391ba40f114ad19711a507971a994b  9   6e1d576301c749bcb7b7943c1782a920  10   Fb60bd4fe28d47cda-bb0eb6834e84e  11   5238ba7d27c7452698e075f64472fadf  12   */ 

The preceding two methods are provided to remove "-" From UUID "-"

 

 1   /** 2   * Obtain a uuid that removes the "-" symbol.  3   *  4   *  @ Return  String UUID  5        */  6       Public   Static  String getuuid (){  7 String S = UUID. randomuuid (). tostring (); 8           //  Remove the "-" symbol  9           Return S. substring (0, 8) + S. substring (9, 13) + S. substring (14, 18) + S. substring (19, 23) + S. substring (24 );  10   }  11   12       /**  13   * Obtain a uuid that removes the "-" symbol.  14   * 15   *  @ Return  16        */  17       Public   Static  String getuuid (){  18 String S = UUID. randomuuid (). tostring ();  19           //  Remove the "-" symbol  20           Return S. Replace ("-","");  21 }

 

UUID consists 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 and then generate a uuid, the first part is different, others 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.

Disadvantage: the unique defect of UUID is that the generated result string is long.

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.