Java UUID without dashes: UUID.randomUUID().toString().replace("-", "")
Codemia Knowledge Hub ·
The simplest efficient way to get a UUID string without dashes in Java is UUID.randomUUID().toString().replace("-", ""), which gives a 32-character hexadecimal string from a cryptographically strong random UUID. If even the replace() overhead matters, format the UUID's getMostSignificantBits() and getLeastSignificantBits() straight into a hex string and skip the intermediate dashed string.
What UUID.randomUUID() generates
UUID.randomUUID() creates a Version 4 UUID: 122 bits of cryptographically secure random data, with the remaining 6 bits encoding the version and variant. The random source is SecureRandom, so the method is suitable for security-sensitive identifiers such as session tokens and API keys.
A standard UUID string looks like this:
The 8-4-4-4-12 grouping is defined in RFC 4122. Removing the four dashes leaves a compact 32-character string.
String.replace(): the recommended approach
For almost every application, replace() is the right choice. It is clear, correct and fast enough.
Output:
String.replace("-", "") scans the 36-character string once and builds a new 32-character string, which takes nanoseconds. Even at millions of UUIDs per second, it is not the bottleneck; SecureRandom inside randomUUID() is the expensive part.
Direct hex formatting into a char array
If you generate UUIDs in a hot loop and profiling shows that string allocation matters, bypass toString() and format the raw bits yourself:
This avoids the intermediate 36-character dashed string. In benchmarks it is roughly 2x faster than replace(), but the absolute difference is in nanoseconds, so use it only if profiling justifies it.
String.format with %016x
A simpler manual approach formats both halves with zero-padding:
%016x pads each half to 16 hex characters. Long.toHexString() does not pad, so if the most significant bits start with zeros its output is shorter than 32 characters. Always zero-pad when converting by hand.
Performance comparison
| Method | Allocations | Relative Speed | Code Complexity |
toString().replace("-", "") | 2 strings (dashed + compact) | Baseline | Minimal |
String.format("%016x%016x", ...) | 1 string + format overhead | Slightly slower (format parsing) | Low |
| Direct char array construction | 1 string (compact only) | About 2x faster | Medium |
| StringBuilder approach | 1 StringBuilder + 1 string | Similar to replace | Low |
UUID.randomUUID() itself takes roughly 1-3 microseconds because it calls SecureRandom.nextBytes(), while the string formatting takes 50-200 nanoseconds. Optimizing the formatting only matters in the unusual case where UUID generation is not the bottleneck.
Third-party libraries
Apache Commons Codec
If you already depend on Apache Commons, its hex utilities work, though there is no dedicated UUID-without-dashes method:
Java UUID Generator (com.fasterxml.uuid)
For high-throughput scenarios, java-uuid-generator offers time-based (v1) and name-based (v3/v5) UUIDs with optimized string generation:
For most projects, the standard java.util.UUID is enough and adds no dependencies.
UUID versions
| Version | Generation Method | Use Case | Dashless String Safe? |
| v1 | Timestamp + MAC address | Time-ordered IDs | Yes |
| v3 | MD5 hash of namespace + name | Deterministic IDs from input | Yes |
| v4 | Cryptographic random | General-purpose unique IDs | Yes |
| v5 | SHA-1 hash of namespace + name | Deterministic IDs (preferred over v3) | Yes |
| v7 | Unix timestamp + random | Time-ordered, sortable, database-friendly | Yes |
Java's UUID.randomUUID() generates v4. For database primary keys, time-sorted v7 UUIDs are increasingly preferred because sequential inserts are friendlier to B-tree indexes. Also, UUIDs from external systems may be v1 and reveal information about the machine that generated them, so don't treat the UUID version as a security assumption.
Storing dashless UUIDs in a database
Choose the column type carefully:
In MySQL, BINARY(16) uses 16 bytes against 32 for CHAR(32), and indexes more efficiently; on tables with millions of rows the index size difference is substantial. Convert a UUID to bytes in Java with:
Thread safety
UUID.randomUUID() is thread-safe, because the underlying SecureRandom handles concurrent access. You don't need synchronization, thread-local instances or object pools:
Pitfalls
Building "UUIDs" from Math.random() or ThreadLocalRandom. These are not cryptographically secure and don't follow the UUID specification. The strings have a higher collision probability and should not be used as unique identifiers in distributed systems.
Parsing dashless strings with UUID.fromString(). It requires the standard 8-4-4-4-12 dashed format, so reinsert the dashes first or use a library:
Related reading
- Efficient swapping of elements of an array in Java
- Ehcache - using a ListInteger as the cache value
- EHCache RMI Replication on JBoss/EC2 throws java.rmi.NoSuchObjectException no such object in table
- EJB 3.1 asynchronous method and thread pool
- EJB's - when to use Remote and/or local interfaces?
- ElasticBeanstalk Java, spring active profile
- ElasticSearch Java API asynchronous writing
- Embedded AMQP Java Broker
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.