Uuid

Common
JVM
JS
Native
2.0
@ExperimentalUuidApi class Uuid : Serializable
(source)

Represents a Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).

A UUID is a 128-bit value used to uniquely identify items universally. They are particularly useful in environments lacking central registration authority or coordination mechanism for generating identifiers, making UUIDs highly suitable for distributed systems.

The standard textual representation of a UUID, also known as the "hex-and-dash" format, is: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where 'x' represents a hexadecimal digit, e.g., "550e8400-e29b-41d4-a716-446655440000". This format includes hyphens to separate different parts of the UUID, enhancing human readability.

This class provides utility functions for:

  • Generating UUIDs.
  • Creating UUIDs from given 128 bits.
  • Parsing UUIDs from and formatting them to their string representations.
  • Converting UUIDs to and from arrays of bytes.
  • Comparing UUIDs to establish ordering or equality.
import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
val uuid = Uuid.parse("550E8400-e29b-41d4-A716-446655440000") // case insensitive
println(uuid) // 550e8400-e29b-41d4-a716-446655440000
//sampleEnd
}
import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
val byteArray = byteArrayOf(
    0x55, 0x0e, 0x84.toByte(), 0x00, 0xe2.toByte(), 0x9b.toByte(), 0x41, 0xd4.toByte(),
    0xa7.toByte(), 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
)
val uuid = Uuid.fromByteArray(byteArray)
println(uuid) // 550e8400-e29b-41d4-a716-446655440000
//sampleEnd
}
import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
// Generates a random and unique uuid each time
val uuid1 = Uuid.random()
val uuid2 = Uuid.random()
val uuid3 = Uuid.random()

println(uuid1 == uuid2) // false
println(uuid1 == uuid3) // false
println(uuid2 == uuid3) // false
//sampleEnd
}

Functions

Common
JVM
JS
Native
1.0

equals

Checks whether this uuid is equal to the specified other object.

fun equals(other: Any?): Boolean
Common
JVM
JS
Native
1.0

hashCode

Returns a hash code value for the object. The general contract of hashCode is:

fun hashCode(): Int
Common
JVM
JS
Native
1.0

toByteArray

Returns a byte array representation of this uuid.

fun toByteArray(): ByteArray
Common
JVM
JS
Native
1.0

toHexString

Returns the hexadecimal string representation of this uuid without hyphens.

fun toHexString(): String
Common
JVM
JS
Native
1.0

toLongs

Executes the specified block of code, providing access to the uuid's bits in the form of two Long values.

fun <T> toLongs(
    action: (mostSignificantBits: Long, leastSignificantBits: Long) -> T
): T
Common
JVM
JS
Native
1.0

toString

Returns the standard string representation of this uuid.

fun toString(): String
Common
JVM
JS
Native
1.0

toULongs

Executes a specified block of code, providing access to the uuid's bits in the form of two ULong values.

fun <T> toULongs(
    action: (mostSignificantBits: ULong, leastSignificantBits: ULong) -> T
): T

Companion Object Properties

Common
JVM
JS
Native
1.0

LEXICAL_ORDER

A Comparator that lexically orders uuids.

val LEXICAL_ORDER: Comparator<Uuid>
Common
JVM
JS
Native
1.0

NIL

The uuid with all bits set to zero.

val NIL: Uuid
Common
JVM
JS
Native
1.0

SIZE_BITS

The number of bits used to represent an instance of Uuid in a binary form.

const val SIZE_BITS: Int
Common
JVM
JS
Native
1.0

SIZE_BYTES

The number of bytes used to represent an instance of Uuid in a binary form.

const val SIZE_BYTES: Int

Companion Object Functions

Common
JVM
JS
Native
1.0

fromByteArray

Creates a uuid from a byte array containing 128 bits split into 16 bytes.

fun fromByteArray(byteArray: ByteArray): Uuid
Common
JVM
JS
Native
1.0

fromLongs

Creates a uuid from specified 128 bits split into two 64-bit Longs.

fun fromLongs(
    mostSignificantBits: Long,
    leastSignificantBits: Long
): Uuid
Common
JVM
JS
Native
1.0

fromULongs

Creates a uuid from specified 128 bits split into two 64-bit ULongs.

fun fromULongs(
    mostSignificantBits: ULong,
    leastSignificantBits: ULong
): Uuid
Common
JVM
JS
Native
1.0

parse

Parses a uuid from the standard string representation as described in Uuid.toString.

fun parse(uuidString: String): Uuid
Common
JVM
JS
Native
1.0

parseHex

Parses a uuid from the hexadecimal string representation as described in Uuid.toHexString.

fun parseHex(hexString: String): Uuid
Common
JVM
JS
Native
1.0

random

Generates a new random Uuid instance.

fun random(): Uuid

Extension Functions

JVM
2.0

toJavaUuid

Converts this kotlin.uuid.Uuid value to the corresponding java.util.UUID value.

fun Uuid.toJavaUuid(): UUID