6 Replies Latest reply: Jun 20, 2008 6:55 AM by 807591

# HashMap , int [][] - key , compareTo override problem , help

i've a hashmap Map<int [][] , Integer> and i want to get the corresponding value for int a[][] from the map.

if i use , map.containsKey(a)
it doesn't compare as required( it doesn't consider order )

so i thought of using class key implementing comparable like this,
``````class key implements Comparable
{
int a[][];
public key(int p[][])
{     a=p;     }

public int compareTo(Object o)
{
key k = (key)o;
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ;j < 3 ; j++)
if(a[i][j]!=k.a[i][j])     return 1;
return 0;
}
}``````
now when i create a key ,
key KK = new key(a) // a is int[][]
map.containsKey(KK) doesn't invoke compareTo method itself ..
don't what the problem is.

but when i try . TreeMap it invokes compareTo method
but don't know why , it acts as before when Map<int [][] , Integer> was used.. any idea

Thanx
• ###### 1. Re: HashMap , int [][] - key , compareTo override problem , help
Comparable is used for sorting/ordering, not for determining if two objects are equal. You need to implement equals (and hashCode) for your keys to properly work in all Maps.
• ###### 2. Re: HashMap , int [][] - key , compareTo override problem , help
u mean i 've to override equals method??

something like this->
``````public boolean equals(Object o)
{
key k = (key)o;
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ; j < 3 ; j++)
if(a[i][j]!=k.a[i][j])     return false;
return true;
}``````
and removing implements Comparable
??
• ###### 3. Re: HashMap , int [][] - key , compareTo override problem , help
You need to override the hashCode() and equals() method for the HashMap. compareTo() is for the TreeMap and not for HashMap. Override the equals() method in a way which has a unique comparison constraints as per your requirement. Also you need to override the hashCode() method, since two equal objects should have the same hashcodes. This is as per the equals/hashCode policy.
Try this also:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html

HTH,
Geet
• ###### 4. Re: HashMap , int [][] - key , compareTo override problem , help
ok, this info seems useful..
but i've never tried overriding hashCode..

searched on the net.. saw a few examples.

wil the following way of overriding hashcode work??
``````class key
{
int a[][];
public key(int p[][])
{     a=p;     }

public boolean equals(Object o)
{
key k = (key)o;
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ; j < 3 ; j++)
if(a[i][j]!=k.a[i][j])     return false;
return true;
}
public int hashCode()
{
return a.hashCode();
}

}``````
ok this will be same if i use TreeMap and implement Comparable<key> and override compareTo() right??

thanx.
• ###### 5. Re: HashMap , int [][] - key , compareTo override problem , help
manishmulani@nitk wrote:
...
wil the following way of overriding hashcode work??
``````class key
{
int a[][];
public key(int p[][])
{     a=p;     }

public boolean equals(Object o)
{
key k = (key)o;
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ; j < 3 ; j++)
if(a[i][j]!=k.a[i][j])     return false;
return true;
}
public int hashCode()
{
return a.hashCode();
}

}``````
NO! When doing that, you will get undesirable results. Execute the following lines of code and examine the output:
``````System.out.println(new int[][]{{1,2},{3,4}}.hashCode());
System.out.println(new int[][]{{1,2},{3,4}}.hashCode());``````
This is better:
``````public int hashCode() {
int hash = 1, mutliplier = 1;
for(int[] row: array) {
for(int val: row) {
hash ^= (val * mutliplier);
mutliplier *= 10;
}
}
return hash;
}``````
(Untested!!!)
ok this will be same if i use TreeMap and implement Comparable<key> and override compareTo() right??
Err, yes, more or less. Of course, a HashMap and TreeMap are not the same.
• ###### 6. Re: HashMap , int [][] - key , compareTo override problem , help
Thanx a lot everyone... i got my problem solved. :)