现在有两个线程
一个线程创建一个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,希望各位不吝赐教。
一个线程创建一个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,希望各位不吝赐教。
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));
}
}
好奇怪啊
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,还望指点
按理只要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.
{
GroupKey g=(GroupKey)o;
return groupname==g.groupname; 改成 groupname.equals(g.groupname);
}
把==改成.equals就没问题了,还是对==和.equal的理解不深刻诶