Search code examples
javauuid

How do I create a unique id for Map in Java?


I am looking for a correct way to create unique ID for Map based on map's contents. So,I expect IDs of 2 maps which contain the same data to be the same as well and have as small chance of collisions as possible.

My current guess is using UUID, writing Map into Object and then building UUID from bytes.

 Map map;
 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 ObjectOutputStream out = new ObjectOutputStream(byteOut);

 out.writeObject(map);
 out.close();
 UUID.nameUUIDFromBytes(byteOut.toByteArray());

However, I wanted to ask if this is the optimal way and if not what else should I try?


Solution

  • To achieve this you can use any suitable for your needs (in terms of collisions, performance) hash function, for example, SHA-1:

    public class MainClas {
    
        public static void main(String[] args) throws NoSuchAlgorithmException {
            Map<String, Integer> map = new HashMap<>();
            map.put("1", 1);
            map.put("2", 2);
            map.put("3", 3);
    
            String mapString = map.entrySet().toString();
            System.out.println(mapString);
    
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            byte[] hashBytes = digest.digest(mapString.getBytes());
    
            String hashString =  bytesToHex(hashBytes);
    
            System.out.println(hashString);
        }
    
        private static String bytesToHex(byte[] hashInBytes) {
            StringBuilder sb = new StringBuilder();
            for (byte b : hashInBytes) sb.append(String.format("%02x", b));
            return sb.toString();
        }
    }
    

    output:

    [1=1, 2=2, 3=3]
    1a1677fe956c66c776a100b32b0a2b20fdabb5f3
    

    P.S. you can minimize collisions by using a composite hash from hashes produced from different algorithms (2 or 3).