java.util.UUID is a class in Java that provides static methods and constructors to manipulate UUIDs (Universally Unique Identifiers). The compareTo() method compares this UUID with the specified UUID based on the value of their UUID's most significant fields first. The package library is java.util.
if(uuid1.compareTo(uuid2) < 0) { System.out.println("uuid1 is less than uuid2"); } else if(uuid1.compareTo(uuid2) == 0) { System.out.println("uuid1 is equal to uuid2"); } else { System.out.println("uuid1 is greater than uuid2"); }
This code generates two random UUIDs and uses the compareTo() method to compare them. It prints out whether uuid1 is less than, equal to, or greater than uuid2.
Example 2: Sorting a list of UUIDs
List uuidList = new ArrayList<>(); uuidList.add(UUID.randomUUID()); uuidList.add(UUID.randomUUID()); uuidList.add(UUID.randomUUID());
uuidList.sort(UUID::compareTo);
This code creates a list of UUIDs, adds three random UUIDs to it, and sorts the list using the compareTo() method. The lambda expression UUID::compareTo is used as the function to compare the UUIDs in the sorting algorithm.
In conclusion, the java.util.UUID class provides a compareTo() method that compares two UUIDs and returns a result based on the value of their most significant fields. It is located in the java.util package library.
Java UUID.compareTo - 17 examples found. These are the top rated real world Java examples of java.util.UUID.compareTo extracted from open source projects. You can rate examples to help us improve the quality of examples.