Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java UUID's long representation

I'm storing tons of Java UUID into a HashMap as row using UUID.toString(). Since the data is huge, soon it throws OutOfMemoryError. Now I'm thinking about a compact way to represent the UUID, preferably something like long, and then later I can easily reconstruct the UUID with that long representation. Is this possible?

like image 936
user468587 Avatar asked Jan 14 '13 17:01

user468587


1 Answers

A UUID is fundamentally a number, but it's a 128-bit number, which is twice the size of a java long. You could use BigInteger (which is probably no more space-efficient than storing UUIDs as strings), or you could encapsulate the UUID in an object that contains two longs — one for the first 64 bits and one for the last 64 bits.

Given the UUID 550e8400-e29b-41d4-a716-446655440000, you would want to create two longs, one containing the number 0x550e8400e29b41d4, and one containing the number 0xa716446655440000.

like image 189
hobbs Avatar answered Oct 28 '22 15:10

hobbs