Order Independent Hash in Java

I'd like to calculate a hash of a set of strings in Java. Yes I can sort the strings and calculate the MD5 hash iterative using digest.update. But I'd prefer to omit the sort and use something like combineUnordered There is a lot of similar question asking the same such as Order-independant Hash Algorithm but non of them provides a simple example showing how to calculate iterative an order independent hash in Java.


  • Just XOR each hash and the order wont matter, plus the hash size will be fixed rather than grow with the size of the collection.

    Hashcode using built in java string hashcode:

    int hashcode =
            .reduce(0, (left, right) -> left ^ right);

    Hashcode using guava and MD5 like the question asked:

    Optional<byte[]> hash =
            .map(s -> Hashing.md5().hashString(s, Charset.defaultCharset()))
            .reduce((left, right) -> xor(left, right));
    static byte[] xor(byte[] left, byte[] right) {
        if(left.length != right.length) {
            throw new IllegalArgumentException();
        byte[] result = new byte[left.length];
        for(int i=0; i < result.length; i++) {
            result[i] = (byte) (left[i] ^ right[i]);
        return result;