How to hash a set of GUIDs in Java

1 Answer

0 votes
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

// A GUID (Globally Unique Identifier) and a UUID (Universally Unique Identifier) 
// are essentially the same, both being 128-bit values used to uniquely identify 
// information in computer systems.

public class HashGuidsExample {

    public static void main(String[] args) {

        // Create a Set containing three randomly generated UUIDs.
        // UUID.randomUUID() produces a type‑4 GUID.
        Set<UUID> guids = Set.of(
                UUID.randomUUID(),
                UUID.randomUUID(),
                UUID.randomUUID()
        );

        // Compute a hash value for the entire set.
        // Objects.hash(...) is idiomatic Java for combining values into a single hash.
        int hash = Objects.hash(guids);

        // Print the GUIDs so we can see what was hashed.
        System.out.println("GUIDs in the set:");
        guids.forEach(System.out::println);

        // Print the resulting hash value.
        System.out.println("\nHash of the GUID set: " + hash);
    }
}



/*
run:

GUIDs in the set:
99bbb374-a78d-464e-aecb-bd4c20adcbab
fda80274-f6a3-4535-af5d-63654046c68b
50b3c2c0-1bfa-4822-9f09-8aed50b45e8a

Hash of the GUID set: 425051440

*/

 



answered Jan 7 by avibootz
...