Notes on the use of UUID

JAVA UUID generation

GUID is a 128-bit long number, generally expressed in hexadecimal. The core idea of ​​the algorithm is to combine the machine's network card, local time, and a random number to generate GUID. In theory, if a machine generates 100,000,000 GUIDs per second, it is guaranteed (probabilistically) for 3240 years to not repeat.

UUID is a new class in 1.5. Under java.util, it can be used to generate a globally unique ID

package com.mytest;

import java.util.UUID;

public class UTest {

    public static void main(String[] args) {

        UUID uuid = UUID.randomUUID();

        System.out.println(uuid);

    }

}

UUID (Universally Unique Identifier) ​​globally unique identifier refers to a number generated on a machine, which is guaranteed to be unique to all machines in the same space and time. Calculations according to the standards set by the Open Software Foundation (OSF) use Ethernet card addresses, nanosecond time, chip ID codes and many possible numbers. Combination of the following parts: current date and time (the first part of UUID is related to time, if you generate a UUID a few seconds after generating a UUID, the first part is different, the rest are the same), the clock Sequence, the globally unique IEEE machine identification number (if there is a network card, obtained from the network card, no network card is obtained in other ways), the only drawback of UUID is that the result string generated will be longer. 

There are mainly the following ways to generate UUID in Java: 

JDK1.5 

If you use JDK1.5, then generating UUID becomes a simple matter, thinking that JDK implements UUID: 

java.util.UUID, just call it directly. 

UUID uuid  =  UUID.randomUUID(); 

String s = UUID.randomUUID().toString();//The primary key id used to generate the database is very good. .   

UUID is composed of a sixteen-digit number, which is expressed in the form of for example 

550E8400-E29B-11D4-A716-446655440000   

//The following is the code to achieve a unique primary key id for the database 

public class UUIDGenerator { 

    public UUIDGenerator() { 

    } 

    /** 

     * Get a UUID 

     * @return String UUID 

     */ 

    public static String getUUID(){ 

        String s = UUID.randomUUID().toString(); 

        // remove the "-" sign 

        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24); 

    } 

    /** 

     * Get the specified number of UUIDs 

     * @param number int The number of UUIDs to get 

     * @return String[] UUID array 

     */ 

    public static String[] getUUID(int number){ 

        if(number < 1){ 

            return null; 

        } 

        String[] ss = new String[number]; 

        for(int i=0;i<number;i++){ 

            ss[i] = getUUID(); 

        } 

        return ss; 

    } 

    public static void main(String[] args){ 

        String[] ss = getUUID(10); 

        for(int i=0;i<ss.length;i++){ 

            System.out.println(ss[i]); 

        } 

    } 

}   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325482001&siteId=291194637