hi people...was working with hashtable's....
code is as follows :

public class X{
		int a;
		int b;
		X(int a, int b){
			this.a = a;
			this.b = b;
		}
		public static void main(String a[]){
			Map map = new HashMap<X,String>();
			map.put(new X(1,2),"some value");
			System.out.println(map.get(new X(1,2)));
		}
}

gettin o/p as null...is there any method i'm supposed to override so as to get the o/p as "some value" ??

Recommended Answers

All 4 Replies

map.put(new X(1,2),"some value")
...creates a new X and uses it as the key
map.get(new X(1,2))
...creates ANOTHER new X (uses the same values as the first one, but it's still a different instance, and therefore != the first one) and tries to find it in the map but (of course) it's not there.

map.put(new X(1,2),"some value")
...creates a new X and uses it as the key
map.get(new X(1,2))
...creates ANOTHER new X (uses the same values as the first one, but it's still a different instance, and therefore != the first one) and tries to find it in the map but (of course) it's not there.

yes cherill....that's right...i understand that part...now how to make the compiler understand that both instances are the same....i mean is there any method i should override....like say hashcode....??was confused about this part...thanks in advance...

Implement interface Comparable

package demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class XTWO implements Comparable<XTWO> {

    int a;
    int b;

    XTWO(int a, int b) {
        this.a = a;
        this.b = b;
    }

//not fully implemented and
// "Note: this class has a natural ordering that is inconsistent with equals."
    public int compareTo(XTWO o) {
        if (o.a == a && o.b == b) {
            return 0;
        } else {
            return -1;
        }
    }
    /*
    public String toString() {
    return new String("[" + a + "," + b + "]");
    }
     */

    public static void main(String a[]) {
        Map map = new HashMap<XTWO, String>();
        // map.put(new X(1, 2), "some value");
        // System.out.println(map.get(new X(1, 2)));
        XTWO x = new XTWO(1, 2);
        map.put(x, "some value");
        System.out.println(map.get(x));
        System.out.println(x);
        System.out.println();
        ///
        Set<XTWO> s = map.keySet();
        for (XTWO elem : s) {
            if (elem.compareTo(x) == 0) {
                System.out.println(map.get(elem));
            }
            System.out.print(elem);
            System.out.print("   ");
            System.out.println(x);
        }
    }
}

Run this example two times. Second time uncomment toString() method.

if you want to use X objects as keys in hashmaps, you don't have to implement Compareable (that's what you do for treemaps).

In hashmaps, the keys are considered equal according to the values of their hahcodes :

class X {
     private int a, b;
     ...
     @Override
     public int hashcode() {
         return a+b+273;//for example
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.