Comparing TreeMap contents gives incorrect answer ! is this correct
===

I use a TreeMap as a 'key' inside another TreeMap
ie

    TreeMap<TreeMap<String, String>, Object>

In my code 'object' is a personal construct, but for this intance I have used a string.

I Have created a pair of TreeMaps to test the TreeMap.CompareTo() and TreeMap.HashCode() methods. this starts with the following...

    public class TreeMapTest 
    
    public void testTreeMap()
    {
    TreeMap<String, String> first = new TreeMap<String, String>();
    TreeMap<String, String> second = new TreeMap<String, String>();
    
    first.put("one", "une");
    first.put("two", "deux");
    first.put("three", "trois");
    second.put("une", "one");
    second.put("deux", "two");
    second.put("trois", "three");
    
    TreeMap<TreeMap<String, String>, String> english = new TreeMap<TreeMap<String, String>, String>();
    TreeMap<TreeMap<String, String>, String> french = new TreeMap<TreeMap<String, String>, String>();
    
    english.put(first, "english");
    french.put(second, "french");

From here I now call the  the english item to see if it contains the key

    if (english.containsKey(second))
    {
    System.out.println("english contains the key");
    //throws error of ClassCastException: Java.util.TreeMap cannot be cast to
    //Java.Lang.Comparable, reading the docs suggests this is the feature if the key is
    //not of a supported type.
    //this error does not occur if I use a HashMap structure for all maps, why is
    //this key type supported for one map structure but not another?
    }

However I should note that both HashMap and TreeMap point to the same HashCode() method in the AbstractMap parent.

My first thought was to convert my TreeMap to a HashMap, but this seemed a bit soppy! So I decided to apply the hashCode() method to the 2 treemap objects.

    int hc1 = first.hashCode();
    int hc2 = second.hashCode();



    if(hc1 == hc2)
    {
    systom.out.printline("values are equal " + hc1 + " " + hc2);
    }

prints the following

    values are equal 3877431 & 3877431

For me the hashcode should be different as the key values are different, I can't find details on the implementation difference of the hashCode() method between HashMap and TreeMap.

Please not the following.
changing the Keys only to HashMap  doesn't stop the ClassCastException error. Changing all the maps to a HashMap does. so there is something  with the containsKey() method in TreeMap that isn't working properly, or I have missunderstood - can anyone explain what?

The section where I get the hashCode of the first and second map objects always produces the same output (no matter if I use a Hash or Tree map here) However the if(english.ContainsKey(second)) doesn't print any message when HashMaps are used, so there is obviously something in the HashMap implementation that is different for the compareTo() method.

My principle questions are.

Where can I find details of the types of keys for use in TreeMap objects (to prevent future 'ClassCastException' errors).

If I can't use a certain type of object as a key, why am I allowed to insert it as a key into the TreeMap in the first place? (surely if I can insert it I should be able to check if the key exists?)

Can anyone suggest another construct that has ordered inster / retriveal to replace my TreeMap key objects?

Or have I potentially found strange behaviour. From my understanding I should be able to do a drop in replacement of TreeMap for HashMap, or have I stumbled upon a fringe scenario?

Thanks in advance for your comments.

David.

ps. the problem isn't a problem in my code as I use a personal utility to create a hash that becomes dependent on the Key and Value pairs (ie I calculate key hash values differently to value hash values... sorry that if is a confusing sentence!) I assume that the hashCode method just sums all the values together without considering if a item is a key or a value.

pps. I'm not sure if this is a good question or not, any pointers on how to improve it?