How to hash a set of GUIDs in Scala

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 {

  def main(args: Array[String]): Unit = {

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

    // Compute a hash value for the entire set.
    // In Scala, 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)

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



/*
run:

GUIDs in the set:
04b83e9c-a08e-44e9-81e4-cc3753799a6e
e08597be-b786-45c9-a530-8fd2dd842930
769d5a34-4534-4bda-b7bd-06b4c5804568

Hash of the GUID set: -159098394

*/

 



answered Jan 7 by avibootz
...