How to hash a set of GUIDs in Kotlin

1 Answer

0 votes
import java.util.UUID

// A GUID (Globally Unique Identifier) and a UUID (Universally Unique Identifier)
// are essentially the same, both being 128-bit values used to uniquely identify
// information in computer systems.

object HashGuidsExample {

    @JvmStatic
    fun main(args: Array<String>) {

        // Create a Set containing three randomly generated UUIDs.
        // UUID.randomUUID() produces a type‑4 GUID.
        val guids: Set<UUID> = setOf(
            UUID.randomUUID(),
            UUID.randomUUID(),
            UUID.randomUUID()
        )

        // Compute a hash value for the entire set.
        // In Kotlin, hashCode() is idiomatic for combining values into a single hash.
        val hash: Int = guids.hashCode()

        // Print the GUIDs so we can see what was hashed.
        println("GUIDs in the set:")
        guids.forEach { println(it) }

        // Print the resulting hash value.
        println("\nHash of the GUID set: $hash")
    }
}


/*
run:

GUIDs in the set:
4989c75c-4612-49ee-b8ff-1e477f9dd7b8
2ca79dc4-dfe7-4233-8f11-f4de342e6f6c
f0ac9bb3-c39c-46f6-8b0c-11ca0331d817

Hash of the GUID set: -863657942

*/



answered Jan 7 by avibootz
...