That means our UUID has a variant of 2.įor variant 2 UUIDs, there’re five different versions: Version Reserved, Microsoft Corporation backward compatibilityįor us, A = 8 (1000), so the first three MSBs are 100. UUID uuid = UUID.nameUUIDFromBytes(byteArr) We can generate a UUID from a byte array using nameUUIDFromBytes(): byte byteArr = It generates a v4 pseudo-random UUIDusing a cryptographically strong pseudo-random number generator: UUID uuid = UUID.randomUUID() Let’s cover the methods in the Java UUID class which we can use to generate the UUID: 1. With fromString(), we can create a UUID from a standard string representation: UUID uuid = omString("533a4559-e55c-18b3-2456-555563322002") This method generates a v3 UUID (name-based). It’ll throw an IllegalArgumentException for any invalid string passed in as an argument. 1: if uuid1 is less than that of uuid2.So, we can use the compareTo()method to compare them: UUID uuid1 = UUID.randomUUID() Īs we know, the compareTo() method returns: Java UUID class implements Comparable interface. We can optionally use the equals() method for comparison as well. Long leastSignificantBits = uuid.getLeastSignificantBits() getLeastSignificantBits() And getMostSignificantBits():Īs the name suggests, getLeastSignificantBits() and getMostSignificantBits() return the 64 least-significant and 64 most-significant bits respectively: UUID uuid = UUID.randomUUID() Let’s cover a few other methods of the Java UUID class: 1. Let’s start by adding a dependency to java-uuid -generator in our POM: We can also query the variant and the version of a UUID: UUID uuid = UUID.randomUUID() Long mostSignificantBits = uuid.getMostSignificantBits() īoth of these methods return a long value. To create a time-based UUID, we’ll have: UUID uuid = Generators.timeBasedGenerator().generate() Note that this library provides various types of UUID generators.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |