📜  Java Java类

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

Java Java类

表示不可变的通用唯一标识符 (UUID) 的类。 UUID 代表一个 128 位的值。这些全局标识符存在不同的变体。此类的方法用于操作 Leach-Salz 变体,尽管构造函数允许创建 UUID 的任何变体(如下所述)。
UUID 有四种不同的基本类型:基于时间、DCE 安全、基于名称和随机生成的 UUID。这些类型的版本值分别为 1、2、3 和 4。

  • 用于在 Web 应用程序中创建会话 ID。它还用于创建事务 ID。
  • 它扩展了对象类。
  • 它实现了 SerializableComparable接口。

构造函数:

UUID(long mostSigBits, long leastSigBits)

使用指定的数据构造一个新的 UUID。
mostSigBits – UUID 的最高有效位
leastSigBits – UUID 的最低有效位

方法:

  • int clockSequence():与此 UUID 关联的时钟序列值。
    14 位时钟序列值由该 UUID 的时钟序列字段构成。时钟序列字段用于保证基于时间的 UUID 中的时间唯一性。
    clockSequence 值仅在基于时间的 UUID 中有意义,其版本类型为 1。如果此 UUID 不是基于时间的 UUID,则此方法将引发 UnsupportedOperationException。
Syntax: public int clockSequence().
Returns: The clock sequence of this UUID.
Exception: 
UnsupportedOperationException - If this UUID is not a version 1 UUID
Java
// Java code illustrating clockSequence() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking clock sequence
        System.out.println(gfg.clockSequence());
    }
}


Java
// Java code illustrating compareTo() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        // generating random UUID
        UUID gfg1 = UUID.randomUUID();
        UUID gfg2 = UUID.randomUUID();
         
        int compare = gfg1.compareTo(gfg2);
        if(compare==1)
            System.out.println("gfg1 is greater than gfg2");
        else if(compare==0)
            System.out.println("both are equal");
        else
            System.out.println("gfg1 is smaller than gfg2");  
    }
}


Java
// Java code illustrating equals() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        // generating random UUID
        UUID gfg1 = UUID.randomUUID();
        UUID gfg2 = UUID.randomUUID();
         
        if(gfg1.equals(gfg2))
            System.out.println("both are equal");
        else
            System.out.println("both are not same");
    }
}


Java
// Java code illustrating fromString() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        // generating random UUID
        UUID gfg = UUID.fromString("e52232e1-0ded-4587-999f-4dd135a4a94f");
        System.out.println("UUID is: " + gfg);
    }
}


Java
// Java code illustrating getLeastSignificantBits() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        UUID gfg = UUID.randomUUID();
         
        // checking the least significant bit
        System.out.println("Least significant bit " +
                gfg.getLeastSignificantBits());
    }
}


Java
// Java code illustrating getMostSignificantBits() bit
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        UUID gfg = UUID.randomUUID();
         
        // checking the most significant bit
        System.out.println("Most significant bit " +
                gfg.getMostSignificantBits());
    }
}


Java
// Java code illustrating hashCode method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        UUID gfg = UUID.randomUUID();
         
        // checking the hash code for this UUID
        System.out.println("Hash code " +
                gfg.hashCode());
    }
}


Java
// Java code illustrating nameUUIDFromBytes() methods
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
         UnsupportedOperationException
    {
        // creating byte array
        byte[] b = {10, 23, 45};
         
        // creating UUID from array
        UUID gfg = UUID.nameUUIDFromBytes(b);
         
        // checking UUID
        System.out.println(gfg);
    }
}


Java
// Java code illustrating node() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) 
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking node value for this UUID
        System.out.println("Node value: "
          + gfg.node());
    }
}


Java
// Java code illustrating randomUUID() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.randomUUID();
         
        // checking  this UUID
        System.out.println("UUID: "
          + gfg);
    }
}


Java
// Java code illustrating timeStamp() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking time stamp for this UUID
        System.out.println("time stamp: "
          + gfg.timestamp());
    }
}


Java
// Java code illustrating toString method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking string format for this UUID
        System.out.println("String equivalent: "
          + gfg.toString());
    }
}


Java
// Java code illustrating variant() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking variant number for this UUID
        System.out.println("variant number is: "
          + gfg.variant());
    }
}


输出:

1691
  • int compareTo(UUID val):将此 UUID 与指定的 UUID 进行比较。
    如果 UUID 不同的最重要字段对于第一个 UUID 更大,则两个 UUID 中的第一个大于第二个。
Syntax: public int compareTo(UUID val).
Returns: -1, 0 or 1 as this UUID is less than, equal to, or greater than val.
Exception: NA.

Java

// Java code illustrating compareTo() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        // generating random UUID
        UUID gfg1 = UUID.randomUUID();
        UUID gfg2 = UUID.randomUUID();
         
        int compare = gfg1.compareTo(gfg2);
        if(compare==1)
            System.out.println("gfg1 is greater than gfg2");
        else if(compare==0)
            System.out.println("both are equal");
        else
            System.out.println("gfg1 is smaller than gfg2");  
    }
}

输出:

gfg1 is smaller than gfg2
  • boolean equals(Object obj):将此对象与指定对象进行比较。当且仅当参数不为 null、是 UUID 对象、具有相同的变体并且包含与此 UUID 逐位相同的值时,结果才为真。
Syntax: public boolean equals(Object obj).
Returns: true if the objects are the same; false otherwise
Exception: NA.

Java

// Java code illustrating equals() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        // generating random UUID
        UUID gfg1 = UUID.randomUUID();
        UUID gfg2 = UUID.randomUUID();
         
        if(gfg1.equals(gfg2))
            System.out.println("both are equal");
        else
            System.out.println("both are not same");
    }
}

输出:

both are not same
  • static UUID fromString(String name):根据 toString() 方法中描述的字符串标准表示创建 UUID。
Syntax: public static UUID fromString(String name).
Returns: a UUID with the specified value.
Exception: 
IllegalArgumentException - If name does not conform to the string 
representation as described in toString()

Java

// Java code illustrating fromString() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        // generating random UUID
        UUID gfg = UUID.fromString("e52232e1-0ded-4587-999f-4dd135a4a94f");
        System.out.println("UUID is: " + gfg);
    }
}

输出:

UUID is: e52232e1-0ded-4587-999f-4dd135a4a94f
  • long getLeastSignificantBits():此方法返回此 UUID 的 128 位值的最低有效 64 位。
Syntax: public long getLeastSignificantBits().
Returns: The least significant 64 bits of this UUID's 128 bit value.
Exception: NA.

Java

// Java code illustrating getLeastSignificantBits() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        UUID gfg = UUID.randomUUID();
         
        // checking the least significant bit
        System.out.println("Least significant bit " +
                gfg.getLeastSignificantBits());
    }
}

输出:

Least significant bit -8406445530268383532
  • long getMostSignificantBits():此方法返回此 UUID 的 128 位值的最高有效 64 位。
Syntax: public long getMostSignificantBits()
Returns: The most significant 64 bits of this UUID's 128 bit value.
Exception: NA

Java

// Java code illustrating getMostSignificantBits() bit
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        UUID gfg = UUID.randomUUID();
         
        // checking the most significant bit
        System.out.println("Most significant bit " +
                gfg.getMostSignificantBits());
    }
}

输出:

Most significant bit 8138958362250724568
  • int hashCode():此方法返回此 UUID 的哈希码。
Syntax: public int hashCode().
Returns: the hash code for this UUID.
Exception: NA.

Java

// Java code illustrating hashCode method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[])
    {
        UUID gfg = UUID.randomUUID();
         
        // checking the hash code for this UUID
        System.out.println("Hash code " +
                gfg.hashCode());
    }
}

输出:

Hash code -2073067668
  • static UUID nameUUIDFromBytes(byte[] name):静态工厂根据指定的字节数组检索类型 3(基于名称)UUID。
Syntax: public static UUID nameUUIDFromBytes(byte[] name)
Returns: A UUID generated from the specified array.
Exception: NA.

Java

// Java code illustrating nameUUIDFromBytes() methods
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
         UnsupportedOperationException
    {
        // creating byte array
        byte[] b = {10, 23, 45};
         
        // creating UUID from array
        UUID gfg = UUID.nameUUIDFromBytes(b);
         
        // checking UUID
        System.out.println(gfg);
    }
}

输出:

f76a74ae-83b6-389c-82ca-8ac0b9febd33
  • long node():与此 UUID 关联的节点值。
    48 位节点值是根据此 UUID 的节点字段构造的。此字段旨在保存生成此 UUID 的机器的 IEEE 802 地址,以保证空间唯一性。
    节点值仅在基于时间的 UUID 中有意义,其版本类型为 1。如果此 UUID 不是基于时间的 UUID,则此方法将引发 UnsupportedOperationException。
Syntax: public long node().
Returns: The node value of this UUID.
Exception: 
UnsupportedOperationException - If this UUID is not a version 1 UUID

Java

// Java code illustrating node() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) 
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking node value for this UUID
        System.out.println("Node value: "
          + gfg.node());
    }
}

输出:

Node value: 138509024482011
  • static UUID randomUUID():静态工厂检索类型 4(伪随机生成)UUID。 UUID 是使用加密强的伪随机数生成器生成的。
Syntax: public static UUID randomUUID().
Returns: randomly generated UUID.
Exception: NA

Java

// Java code illustrating randomUUID() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.randomUUID();
         
        // checking  this UUID
        System.out.println("UUID: "
          + gfg);
    }
}

输出:

UUID: 937418f1-f1b6-4f7a-b9f6-9fa51ba780e3
  • long timestamp():与此 UUID 关联的时间戳值。
    60 位时间戳值由该 UUID 的 time_low、time_mid 和 time_hi 字段构成。生成的时间戳从 UTC 1582 年 10 月 15 日午夜开始以 100 纳秒为单位进行测量。
    时间戳值仅在基于时间的 UUID 中有意义,其版本类型为 1。如果此 UUID 不是基于时间的 UUID,则此方法将引发 UnsupportedOperationException。
Syntax: public long timeStamp().
Returns: the time stamp value.
Exception: NA.

Java

// Java code illustrating timeStamp() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking time stamp for this UUID
        System.out.println("time stamp: "
          + gfg.timestamp());
    }
}

输出:

time stamp: 137004589606850094
  • String toString():此方法返回表示此 UUID 的 String 对象。
Syntax: public String toString().
Returns: a string object for this UUID.
Exception: NA

Java

// Java code illustrating toString method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking string format for this UUID
        System.out.println("String equivalent: "
          + gfg.toString());
    }
}

输出:

String equivalent: c81d4e2e-bcf2-11e6-869b-7df92533d2db
  • int variant():与此 UUID 关联的变体编号。变体编号描述了 UUID 的布局。
Syntax: public int variant()
Returns: The variant number of this UUID
Exception: NA

Java

// Java code illustrating variant() method
import java.util.UUID;
class UUIDdemo
{
    public static void main(String arg[]) throws
            UnsupportedOperationException
    {
        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
         
        // checking variant number for this UUID
        System.out.println("variant number is: "
          + gfg.variant());
    }
}

输出:

variant number is: 2
  • int version():与此 UUID 关联的版本号。版本号描述了这个 UUID 是如何生成的。版本号的含义如下:
    • 1 基于时间的 UUID
    • 2 DCE 安全 UUID
    • 3 基于名称的 UUID
    • 4 随机生成的 UUID