Java UUID nameUIDFromBytes()方法及示例
Java中UUID类的nameUIDFromBytes()方法通常用于根据指定的字节数组检索一个基于第三类名称的UUID。这被用作静态工厂方法。
语法
public static UUID nameUUIDFromBytes(byte[] byte_name)
参数: 该方法接受一个参数byte_name,它指的是用来构建UUID的字节数组。
返回值: 该方法返回一个由指定数组生成的UUID实例。
下面的程序说明了nameUIDFromBytes()方法的工作。
程序1 :
// 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
程序2
// 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