Skip to main content
S

How to Generate UUID in Scala

Using java.util.UUID

How to Generate UUID in Scala. Call UUID.randomUUID() to generate a Scala UUID in one line. Since values are plain strings, a dart uuid check validates the format with a static helper instead of parsing into an object.

Generate UUID in Scala

UUID.randomUUID()
550e8400-e29b-41d4-a716-446655440000

Scala runs on the JVM, so UUID generation in Scala means java.util.UUID — the language doesn't ship a separate UUID type of its own. Call UUID.randomUUID() and you get a random, RFC 4122-compliant 128-bit identifier with zero extra dependencies, using the same API Java developers have relied on for years. Below you'll find copy-paste ready code for generating a Scala UUID, safely parsing a string into one with Try, and where to look when you need a sortable v7 format.

How to Generate a UUID in Scala

The idiomatic way to generate a UUID in Scala is UUID.randomUUID() via Java interop — Scala has no separate UUID type of its own, so you call the JVM's java.util.UUID class directly. Assign the result to a val and let Scala's type inference handle the rest. When a stable identifier is needed for a name, pairing the python uuid helper with uuid5() and NAMESPACE_DNS gives the same 128-bit value every time.

Main.scala
import java.util.UUID

val myUuid: UUID = UUID.randomUUID()
println(myUuid)
// Output: 550e8400-e29b-41d4-a716-446655440000

Explanation

Convert a String to a UUID in Scala

Parsing a UUID string in Scala uses the same UUID.fromString() method as Java, which throws an IllegalArgumentException on malformed input rather than returning a safe result. Malformed input from a REST request or a database column is easier to handle once a java uuid example wraps parsing in a try/catch.

Main.scala
import java.util.UUID
import scala.util.Try

val str = "550e8400-e29b-41d4-a716-446655440000"

// Convert the exception-throwing Java API into a safe Option
val maybeUuid: Option[UUID] = Try(UUID.fromString(str)).toOption

maybeUuid match {
  case Some(uuid) => println(s"Valid UUID: $uuid")
  case None       => println("Invalid UUID string")
}

Explanation

Other Options for Generating UUIDs in Scala

java.util.UUID has no native support for UUID version 7, the newer time-sortable format many databases now prefer for primary keys. Since Scala compiles to the JVM, projects that need v7 identifiers reach for the same options available to any Java codebase.

build.sbt
libraryDependencies += "com.github.f4b6a3" % "uuid-creator" % "6.0.0"
Main.scala
import com.github.f4b6a3.uuid.UuidCreator

// Version 7 - Unix timestamp + random, sortable and index-friendly
val uuidV7 = UuidCreator.getTimeOrderedEpoch()
println(uuidV7)

Explanation

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!