1

Is there any way to use byte array as a key in BTreeMap like this:

BTreeMap<byte[], Integer> myBTreeMap = db.getTreeMap("myBTreeMap");

Currently this exception is thrown when trying to put new object into the map:

Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to java.lang.Comparable ...

What is proper way of making this to work? I would like to know solution without using wrapper classes.

Any ideas are welcome.

[UPDATE]

I've used proposed solution by SJuan76:

    BTreeMap<byte[], Integer> myBTreeMap = db.createTreeMap("myBTreeMap")
            .comparator(SignedBytes.lexicographicalComparator())
            .makeOrGet();

Used comparator can be found in Guava library if needed.

PrimosK
  • 13,848
  • 10
  • 60
  • 78
  • Use a wrapper class for byte arrays that implements `Comparable`, e.g. a `ByteBuffer` or a custom wrapper class. – Thomas Jul 10 '14 at 07:27

3 Answers3

2

MapDB provides byte[] array comparator: Fun.BYTE_ARRAY_COMPARATOR

Here is an example howto use in code:

   Map<byte[], Object> map = db.createTreeMap("map")
               .comparator(Fun.BYTE_ARRAY_COMPARATOR)
               .makeOrGet();
Jan Kotek
  • 1,084
  • 7
  • 4
1

Looking at the Javadoc (I guess it is the Javadoc for that project and not another one, it would have been nice of you to link to it) there is defined a constructor that takes a Comparator<K>. Use that constructor and pass it a Comparator<byte[]> that gives the ordering you desire.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Also, Thomas comment is also a valid option (but I would prefer dealing directly with the raw types instead of wrappers). – SJuan76 Jul 10 '14 at 07:29
1

I think you must wrap the byte array in a class that implements Comparable:

import org.apache.commons.lang.builder.CompareToBuilder;
public class Key implements Comparable<Key> {
    private final byte[] data;

    public Key(final byte[] data) {
        this.data = data;
    }

    public int compareTo(final Key k) {
        return new CompareToBuilder().append(data, k.data).toComparison();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getOuterType().hashCode();
        result = prime * result + Arrays.hashCode(data);
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Key other = (Key) obj;
        if (!getOuterType().equals(other.getOuterType())) {
            return false;
        }
        if (!Arrays.equals(data, other.data)) {
            return false;
        }
        return true;
    }

    private Test getOuterType() {
        return Test.this;
    }

}
rafalopez79
  • 2,046
  • 4
  • 27
  • 23