现在有两个线程
一个线程创建一个group对象,并给他创建的一相应的key
把他们装入一个hashmap里
代码如下 group=new Group(str_str[1]);
 //将这个线程类加入到group的clientthread里
 group.clientthread.add(this);
 //打印group的内容
 System.out.println(group);
 //生成这个group对象的一个相应的key
 GroupKey gk=new GroupKey(str_str[1]);
 //打印key的值
 System.out.println(gk);
//把这一对键值对加入到serverThread的grouphashmap里
serverThread.grouphashmap.put(gk,group);
//打印grouphashmap的内容
System.out.print(serverThread.grouphashmap);                                    打印出的内容为
Group@253498
GroupKey@1c0
{GroupKey@1c0=Group@253498}第二个线程要用一个key找到对应的值
//首先打印grouphashmap的内容
System.out.println(serverThread.grouphashmap);
//生成一个groupkey和gk一样
GroupKey gk1=new GroupKey(str_str[1]);
//打印一下gk1
System.out.println(gk1);
//在grouphashmap里得到gk1所对应的value
 System.out.println(serverThread.grouphashmap.get(gk1));//false
创建的gk1和第一个线程的gk是一样的我原本是要找到第一个线程加进hashmap里的group
可是打印结果很奇怪
{GroupKey@1c0=Group@253498}
GroupKey@1c0
null打印出的group对象和kgroupkey对象都和我放进去的一样
可是在用hashmap的get()方法取group的对象的时候
却是null
这是为什么呢,菜鸟第一次用hashmap,希望各位不吝赐教。

解决方案 »

  1.   

    这个应该是java堆栈的问题吧,我认为第二个key(gk1)虽然值与gk一样,但是地址已经不一样了,那么自然就找不到对应的value了。不应该新建一个key,而是直接引用第一个key(gk1)
      

  2.   

    写了一个模拟的代码这个运行两个线程就行
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;class HashMapTest
    {
            
    static HashMap hm=new HashMap();
            public static void main(String[] args)
            {
             Thread th1=new mythread(hm);
             th1.start();
             try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
              System.out.print( hm);
              Thread th2=new mythread1(hm);
              th2.start();
              
            }
    }class Student
    {
            int num;
            String name;
            Student(int num,String name)
            {
                    this.num=num;
                    this.name=name;
            }
                    
           Student(String name)
           {
            num=20;
            this.name=name;
           }
            
            public String toString()
            {
                    return num+":"+name;
            }
    }
    class studentkey
    {
    String   name;
    studentkey(String num)
    {
    name=num;

    }
     public int hashCode()
        {
     
      char[]chars=name.toCharArray(); //把字符中转换为字符数组 
      int code=0;

      for(int i=0;i<chars.length;i++){//输出结果
             code=code+(int)chars[i];
            }
     
                return code;
        }
                
       
    public boolean equals(Object o)
        {
                studentkey s=(studentkey)o;
                return name==s.name;
        }
    }
    class mythread extends Thread{
    HashMap hm=new HashMap();
    mythread(HashMap hm2)
    {
      
            this.hm=hm2;

    }
    void set()
    {

    }
    public void run()
    {
     String name="zhang";
             Student s1=new Student(name);
             studentkey sk1=new studentkey(name);
            
             hm.put(sk1, s1);
             System.out.println(hm);
             
             
     
    }
    }
    class mythread1 extends Thread
    {
    HashMap hm=new HashMap();
    mythread1(HashMap hm)
    {
      
            this.hm=hm;   

    }
    public void run()
    {
    String name="zhang";
      studentkey sk2=new studentkey(name);
              System.out.println(hm.containsKey(sk2));
              
              System.out.print( hm.get(sk2));   
    }

    }
    好奇怪啊
      

  3.   

    GroupKey 的hashCode方法和equals方法是怎么写的“生成一个groupkey和gk一样”这两个对象的hashCode是否一样,调用equals是否相等
      

  4.   

    这是我groupkey类的代码
    class GroupKey
    {
    String groupname;
    GroupKey(String gn)
    {
    groupname=gn;
    }
     public int hashCode()
         {
      char[]chars=groupname.toCharArray(); //把字符中转换为字符数组 
      int code=0;
      //System.out.println("\n\n汉字 ASCII\n----------------------");
      for(int i=0;i<chars.length;i++){//输出结果          //System.out.println(" "+chars[i]+" "+(int)chars[i]);
             code=code+(int)chars[i];
            }
                return code;
         }
                 
         public boolean equals(Object o)
         {
              GroupKey g=(GroupKey)o;
                 return groupname==g.groupname;
         }
         
    }hashcode equals 都很简单
    而且第二次生成gk1,打印出来的值和gk一样,刚刚接触hashmap,还望指点
      

  5.   

    我用其它对象代替Group和Hashmap就正常的,第一个程序完整的给出来,就可以试下看看了。
    按理只要gk1.equals(gk) == true 就能得到value了public V get(Object key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. 
    More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.) A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases. 
      

  6.   

      public boolean equals(Object o)
         {
              GroupKey g=(GroupKey)o;
                 return groupname==g.groupname; 改成 groupname.equals(g.groupname);
         }
      

  7.   


    把==改成.equals就没问题了,还是对==和.equal的理解不深刻诶
      

  8.   

    哦对,我用的groupname都是interned常量