黑马程序员技术交流社区
标题:
Map集合问题,求教!!
[打印本页]
作者:
王磊
时间:
2013-6-16 06:42
标题:
Map集合问题,求教!!
本帖最后由 孙百鑫 于 2013-6-19 09:04 编辑
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Student s)
{
if (this.getName().compareTo(s.getName()) == 0)
{
return this.getAge() - s.getAge();
}
return this.getName().compareTo(s.getName());
}
public int HashCode()
{
return name.hashCode() + age * 34;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Student))
{
throw new ClassCastException("类型不匹配");
}
Student s = (Student) obj;
return this.name.equals(s.name)&&this.age == s.age;
}
}
class MapTest2
{
public static void main(String[] args)
{
HashMap<Student,String> map = new HashMap<Student,String>();
map.put(new Student("zhangsan1",20),"beijing1");
map.put(new Student("zhangsan2",21),"beijing2");
map.put(new Student("zhangsan3",22),"beijing3");
map.put(new Student("zhangsan4",23),"beijing4");
map.put(new Student("zhangsan3",22),"beijing3");
map.put(new Student("zhangsan4",23),"beijing4");
Set<Student> ts = map.keySet();
for (Iterator<Student> it = ts.iterator() ;it.hasNext() ; )
{
Student stu = it.next();
System.out.println("name:" + stu.getName() + ",age:" + stu.getAge() + ",addr:" + map.get(stu) );
}
/*
Set<Map.Entry<Student,String>> ts = map.entrySet();
for (Iterator<Map.Entry<Student,String>> it = ts.iterator() ;it.hasNext() ; )
{
Map.Entry<Student,String> me = it.next();
System.out.println("name:" + me.getKey().getName() + ",age:" + me.getKey().getAge() + ",addr:" + me.getValue() );
}
}
}
复制代码
自己敲了一边发现编译能够通过,但是不能消除重复。又重新看了下视频,发现Student类中的比较方法跟老师写的基本一样,实在是找不出原因。求教各位帮帮忙?
作者:
王磊
时间:
2013-6-16 06:45
75~83行是被注释掉的,刚刚发帖的时候没有删掉。就当做不存在好了。。。谢谢各位!!
作者:
kaka小明
时间:
2013-6-16 08:32
LZ我只是发现了用HashMap时,student类的hashCode(),compareTo()和equals()均没有执行,这个我也不知道为什么,这个问题还请大神解答。我尝试用TreeMap,结果就出来了。见代码:
import java.util.*;
class Student1 implements Comparable<Student1>
{
private String name;
private int age;
Student1(String name,int age)
{
//System.out.println("构造器执行");
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Student1 s)
{
System.out.println("比较方法执行");
if (this.getName().compareTo(s.getName()) == 0)
{
return this.getAge() - s.getAge();
}
return this.getName().compareTo(s.getName());
}
public int HashCode()
{ System.out.println("hashcode方法执行。。");
return name.hashCode() + age * 34;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Student1))
{
throw new ClassCastException("类型不匹配");
}
Student1 s = (Student1) obj;
boolean res=this.name.equals(s.name)&&this.age == s.age;
System.out.println(this.toString()+"和"+s.toString()+"相同吗?"+res);
return res;
}
}
class MapTest2
{
public static void main(String[] args)
{
// HashMap<Student1,String> map = new HashMap<Student1,String>();//hashcode compareto和equals方法均为执行!!
TreeMap<Student1,String> map = new TreeMap<Student1,String>();
map.put(new Student1("zhangsan1",20),"beijing1");
map.put(new Student1("zhangsan2",21),"beijing2");
map.put(new Student1("zhangsan3",22),"beijing3");
map.put(new Student1("zhangsan4",23),"beijing4");
map.put(new Student1("zhangsan3",22),"beijing3");
map.put(new Student1("zhangsan4",23),"beijing4");
Set<Student1> ts = map.keySet();
for (Iterator<Student1> it = ts.iterator() ;it.hasNext() ; )
{
Student1 stu = it.next();
System.out.println("name:" + stu.getName() + ",age:" + stu.getAge() + ",addr:" + map.get(stu) );
}
}
}
作者:
王磊
时间:
2013-6-16 12:57
非常感谢,我又找了一遍,发现问题了,原来是笔误,在覆写hashCode方法的时候,把hashCode的首字母写成大写的了。。。丢人了。。。还是很感谢你。
作者:
花伟昌
时间:
2013-6-16 22:13
public int HashCode()//此处大写错误,没有完成覆写 hashCode()方法。
{
return name.hashCode() + age * 34;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2