Uuid
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.
Since Kotlin
2.0Samples
import kotlin.uuid.*
fun main() {
//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() {
//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() {
//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
Returns the hexadecimal string representation of this uuid without hyphens.
Converts this kotlin.uuid.Uuid value to the corresponding java.util.UUID value.