Java Comparator for byte array (lexicographic)

15,400

Solution 1

Using Guava, you can use either of:

The UnsignedBytes comparator appears to have an optimized form using Unsafe that it uses if it can. Comments in the code indicate that it may be at least twice as fast as a normal Java implementation.

Solution 2

Found this nice piece of code in Apache Hbase:

    public int compare(byte[] left, byte[] right) {
        for (int i = 0, j = 0; i < left.length && j < right.length; i++, j++) {
            int a = (left[i] & 0xff);
            int b = (right[j] & 0xff);
            if (a != b) {
                return a - b;
            }
        }
        return left.length - right.length;
    }
Share:
15,400

Related videos on Youtube

marcorossi
Author by

marcorossi

Updated on July 03, 2020

Comments

  • marcorossi
    marcorossi almost 4 years

    I have a hashmap with byte[] keys. I'd like to sort it through a TreeMap.

    What is the most effective way to implement the comparator for lexicographic order?

  • ColinD
    ColinD about 13 years
    This is basically what the non-optimized version of Guava's UnsignedBytes.lexicographicalComparator() does.
  • Deepak
    Deepak about 13 years
    do we have the solution in "Java",if so please post a working example.
  • marcorossi
    marcorossi about 13 years
    As ColinD says in the comment to my answer, my solution is the same as the non optimized one in Guava. So you can straight use mine, which is a working example, or follow ColinD's links.
  • Lukas Eder
    Lukas Eder over 11 years
    Hmm, why did they use i and j, when one variable would've been sufficient. Also, storing int length = Math.min(left.length, right.length) and comparing i < length would improve this for large arrays
  • marcorossi
    marcorossi almost 11 years
    you would expect that the length field of the array would be as expensive