I’m using a TreeMap (SortedMap) whose keys are Object[] with elements of varying types.
TreeMap’s equals() doesn’t work on Object[] like Arrays’s equals() would do — which means it won’t work when using its methods like containsKey() and get() unless I workaround it.
Is there somewhere a solution for this that doesn’t involve creating a whole new Class?
EDIT :
Just to make it clear, I made a mistaken assumption. Creating a new Comparator(){} also does affect every method that uses equality, such as equals(), not only the tree sorter.
While I agree with Matt Ball that you generally shouldn’t use mutable (changeable) types as your keys, it is possible to use a
TreeMapin this manner as long as you are not planning on modifying the arrays once they are in the tree.This solution does involve the creation of a class, but not a new
Mapclass, which is what it seems you are asking. Instead, you would need to create your own class whichimplements Comparator<Object[]>that can compare arrays. The class could use theArrays.equals()method to determine if they are equal, but would need to also have a consistent rule to determine which array comes before another array when the arrays are not equal.