KEY:
static private String name

解决方案 »

  1.   

    class Key {
      private Key instance = new Key();
    private String name;
    private Key() {
    }
     
    public Key getInstance() {
       return instance;
    }public void setName(string name) { this.name = name;}}
      

  2.   

    能详细的说一下你加 equals方法的过程吗?
      

  3.   

    import java.util.Map;
    import java.util.HashMap;public class TestQuestion {
        public static void main(String args[]) {
            Map map = new HashMap();
            map.put(new Key(5),new Integer(12));
            Object o = map.get(new Key(5));
            if (o instanceof Integer) {
              System.out.println("got value = "+(Integer)o);
            } else {
              System.out.println("got no object");
            }
        }
    }class Key {
        private int name;   
        Key(int name){
    this.name = name;
    }
        public boolean equals(Object o){
         return (o instanceof Key)
         && (name == (((Key)o).name));
        }
        public int hashCode(){
         return name;    
        }
    }
    以上可以正确的得到你想要的对象-----------------------------------------------------------
      

  4.   

    class Key {
      private Key instance = new Key();
    private String name;
    private Key() {
    }
     
    public Key getInstance() {
       return instance;
    }public void setName(string name) { this.name = name;}}
    ---------------------------------------------------
    不好意思,忘了说明一点,不能修改调用Key这个类的构造方法,也就是必须用 new Key("参数")
    但可以在Key的内部声明其他形式的构造方法,所以用 getInstance()方法也不行
      

  5.   

    map.put(new Key("TEST"),new Integer(12));
    Object o = map.get(new Key("TEST"));
    -------------------------------------------------
    你这两句new 了两个对象,虽然都是用的是"TEST",
    但是两个对象的hashCode却不是一样的,在HashMap中
    的get方法会先调用开始put进去的key的equals()和hashCode()
    这两个方法,如果你是用你自己写的类来作key,那么你得重写
    这两个方法,不然它们缺省是用Object的equals和hashCode,
    就像你刚刚put时new了一个对象,get时又new了一个对象,
    明明放进去了,怎么得不到呢?问题就在这里,所以一般
    很少用自己写的类去做key的,可以用Integer或String来做key,
    一旦你要用自己写的来做,那你得重新equals和hashCode这两个方法
      

  6.   

    To: fog628(发粪涂墙)
    你的代码我试过了,打印的结果还是
    got no object
    而不是
    got value = 这段程序的最终目的就是想把放进去的匿名实例再取出来,但取出来的时候也是产生一个实例的过程
    所以,要求只能修改Key这个类来实现类似
    Key k = new Key("Test")
    map.put(k,new Integer(12));
    Object o = map.get(k);
    这样的功能!
      

  7.   

    呵呵,不会啊,我的结果:got value = 12我的程序就可以实现你上面的功能了
      

  8.   

    class Key  {
        private String name;
        public Key(String name) {
          this.name = name;
        }
        
        public int hashCode() {
         return name.hashCode();
        }
        
        public boolean equals(Object obj) {
         return name.equals(((Key)obj).name); 
        }
    }
      

  9.   

    同意 fog628(发粪涂墙) 的说法。
      

  10.   

    楼上的不行啊,你用String的hashCode()来做Key的hashCode请看下面:
    import java.util.Map;
    import java.util.HashMap;public class TestQuestion {
        public static void main(String args[]) {
            Map map = new HashMap();
            map.put(new Key("TEST"),new Integer(12));
            Object o = map.get(new Key(new String("TEST")));
            if (o instanceof Integer) {
              System.out.println("got value = "+(Integer)o);
            } else {
              System.out.println("got no object");
            }
        }
    }class Key {
        private String name;   
        Key(String name){
    this.name = name;
    }
        public boolean equals(Object o){
         return (o instanceof Key)
         && (name == (((Key)o).name));
        }
        public int hashCode(){
         return name.hashCode();    
        }
    }
    ----------------------------------------------
    Object o = map.get(new Key(new String("TEST")));这样得出来的hashCode就不一样了
      

  11.   

    new Key("参数")程序要求这个参数类型必须是String的fog628(发粪涂墙)
    你的代码那个参数是int型的,hashCode()返回类型也是int型的啊
      

  12.   

    To :  takecare(大厅)你的方法不行,但是我的意思就是这样了,传进去的参数必须是String类型的!
      

  13.   

    hashCode of String
    public int hashCode()
    Returns a hash code for this string. The hash code for a String object is computed as 
     s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     
    using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.) Overrides:
    hashCode in class Object
    Returns:
    a hash code value for this object.
    See Also:
    Object.equals(java.lang.Object), Hashtable
    所以只要内容一样String的hashCode总是一样的。
      

  14.   

    import java.util.Map;
    import java.util.HashMap;public class TestQuestion {
        public static void main(String args[]) {
            Map map = new HashMap();
            map.put(new Key("TEST"),new Integer(12));
            Object o = map.get(new Key(new String("TEST")));
            if (o instanceof Integer) {
              System.out.println("got value = "+(Integer)o);
            } else {
              System.out.println("got no object");
            }
        }
    }class Key {
        private String name;   
        Key(String name){
    this.name = name;
    }
        public boolean equals(Object o){
         return (o instanceof Key)
         && (name == (((Key)o).name));
        }
        public int hashCode(){
         return name.hashCode();    
        }
    }
    To :  takecare(大厅)
    你的代码执行结果并不打印出 got value = 12
      

  15.   

    汗一个,hashcode是一样的,但是这个是不一样的:
    public boolean equals(Object o){
         return (o instanceof Key)
         && (name == (((Key)o).name));
        }
    name == (((Key)o).name这个能用 ==吗?class Key {
    private String name;
    Key(String name) {
    this.name = name;
    }
    public boolean equals(Object o) {
    return (o instanceof Key) && (name.equals(((Key) o).name));
    }
    public int hashCode() {
    System.out.println("name-"+name+"--hash:"+name.hashCode());
    return name.hashCode();
    }
    }
      

  16.   

    class Key {
        private String name;   
        Key(String name){
    this.name = name;
    }
        public boolean equals(Object o){
         return (o instanceof Key)
         && (name.equals(((Key)o).name));
        }
        public int hashCode(){
         System.out.println(name.hashCode());
         return name.hashCode();    
        }
    }
      

  17.   

    刚刚不好意思,把equals写成==了,
      

  18.   

    谢谢大家,问题解决了但我还想问一下,下面这条语句调用equals,我不太明白,这个本来就是在重写equals方法,这样调用不是
    成了嵌套调用?不会导致错误嘛!return (o instanceof Key)&& (name.equals(((Key)o).name));
      

  19.   

    那是调用String的equals,是比较内容的。
      

  20.   

    这么看来。instanceof 应该和equals方法没有关系吧.
      

  21.   

    return name.hashcod
    意思不是反回String所在的地址位置吗?是 class Key == String name 的意思吗?
      

  22.   

    HashMap 使用的是类的 hashCode()  来识别是不是同一个类!你自己的 Key 类又没有定义 hashCode() 这个方法,它是使用 Object.hashCode() 方法,返回的是类的内存地址.你说你 两次 new Key() ,怎么可能是同一个实例呢?
    在 Java 里,无论什么对象,只要使用 new 语句就必然会产生一个真正的实例.而且你的这句本来就不对:
    Object o = map.get(new Key(new String("TEST")));你自己都强制指定 JVM 去 new 一个 Key() ,怎么可能不产生一个新的实例呢?
    要是不改写你写的 TestQuestion  类中的代码,你说的功能根本就是不可能实现的!!
    除非你不用 Java 语言,呵呵!
      

  23.   

    Object o = map.get(new Key(new String("TEST")));
    Object o = map.get(new Key("TEST"));
    难道你说上面这两语句的作用不一样,
    new String("TEST") 和 "TEST"都是产生了一个新的字符串对象,只是它们在内存中的存储方式不太一样罢了!
    Object o = map.get(new Key(new String("TEST")));
    这句话的意思就是让JVM去new 一个新的Key实例,但我要的功能是在new Key这个实例时会检测是否已存在参数相同的实例,如果存在,就让这个新的也指向它,所以,用hashCode()是可以完成这样功能的!
    至少我用下面的代码做了好多测试是正确的.最终的代码是这样的,这就可以达到我说的效果,也就是在产生 new Key("参数")时,只要参数不变,你产生再多次,都是一个实例.import java.util.Map;
    import java.util.HashMap;public class TestQuestion {
        public static void main(String args[]) {
            Map map = new HashMap();
            map.put(new Key("TEST"),new Integer(12));        Object o = map.get(new Key("TEST"));
            if (o instanceof Integer) {
              System.out.println("got value = "+(Integer)o);
            } else {
              System.out.println("got no object");
            }
        }
    }
    class Key {
    private String name;
    Key(String name) {
    this.name = name;
    }
    public boolean equals(Object o) {
    return (o instanceof Key) && (name.equals(((Key) o).name));
    }
    public int hashCode() {
    System.out.println("name ==> "+name+"   hash ==> "+name.hashCode());
    return name.hashCode();
    }
    }
      

  24.   

    用单态模式:构造函数private,在构造函数里产生一个object,然后用一个Instance()调用构造函数.返回一个唯一的一个对象!
      

  25.   

    To: programeyonger
    你所说的单态模式是可以完成唯一实例这个功能的,但我现在的代码是只能修改Key这个类,TestQuestion这个类不能做任何修改,也就是调用产生Key的时候必须用new Key("参数")这样的形式谢谢
      fog628(发粪涂墙) 
      takecare(大厅) 
    二位的指点....