📜  UUID nameUUIDFromBytes() Java中的方法及示例

📅  最后修改于: 2022-05-13 01:55:13.839000             🧑  作者: Mango

UUID nameUUIDFromBytes() Java中的方法及示例

Java中UUID类nameUUIDFromBytes()方法一般用于根据指定的字节数组来检索第三种基于名称的UUID。这用作静态工厂方法。

句法:

public static UUID nameUUIDFromBytes(byte[] byte_name)

参数:此方法接受参数byte_name ,该参数引用用于构造 UUID 的字节数组。

返回值:此方法返回从指定数组生成的UUID 实例

下面的程序说明了 nameUUIDFromBytes() 方法的工作:

方案一:

// Java code to illustrate nameUUIDFromBytes() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating a byte array
        byte[] byte_name
            = { 50, 40, 30, 20, 10 };
  
        // Printing the byte[]
        System.out.println("Specified byte array: "
                           + Arrays.toString(byte_name));
  
        // Creating an UUID from byte
        UUID UU_ID
            = UUID
                  .nameUUIDFromBytes(byte_name);
  
        // Displaying the UUID value
        System.out.println("UUID value from byte: "
                           + UU_ID);
    }
}
输出:
Specified byte array: [50, 40, 30, 20, 10]
UUID value from byte: d66541c4-a9db-3308-8c67-bbf87dc0df8b

方案二:

// Java code to illustrate nameUUIDFromBytes() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating a byte array
        byte[] byte_name
            = { 10, 15, 1, 45, 13, 20, 71 };
  
        // Printing the byte[]
        System.out.println("Specified byte array: "
                           + Arrays.toString(byte_name));
  
        // Creating an UUID from byte
        UUID UU_ID
            = UUID
                  .nameUUIDFromBytes(byte_name);
  
        // Displaying the UUID value
        System.out.println("UUID value from byte: "
                           + UU_ID);
    }
}
输出:
Specified byte array: [10, 15, 1, 45, 13, 20, 71]
UUID value from byte: 15fe1179-e857-306b-ad67-b2388e006c8a