HM周磊 发表于 2013-3-28 17:24
楼上威武,一语惊醒梦中人啊!!!但是,编译没问题了,运行却报了错!!
你的真是太麻烦了。
我真看了你的需求,然后我回故了一下HashMap.这里实现类的比较性没有作用。带Tree的集合才需要。- package day1;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- public class Program5 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- HashMap<Student,String> personList=new HashMap<Student,String>();
- personList.put(new Student("Tom",21), "上海");
- personList.put(new Student("Jack",21), "北京");
- personList.put(new Student("kity",25), "南京");
- personList.put(new Student("Boky",19), "武汉");
- personList.put(new Student("Boky",19), "武汉");
- for(Object obj:personList.entrySet()){
- Map.Entry<Student, String> me=(Map.Entry<Student, String>)obj;
- System.out.println(me.getKey()+":"+me.getValue());
- }
- }
- }
- class Student
- {
- private String name;
- private int age;
- public Student(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int hashCode(){
- System.out.println("come to hashCode");
- return 1;
- }
- /*@Override
- public boolean equals(Object obj){
- System.out.println("come to equals");
- if(!(obj instanceof Student))
- throw new RuntimeException();
- Student stu=(Student)obj;
- return this.name.contains(stu.name);
- }*/
- // @Override
- // public int compareTo(Student o) {
- // int num=o.age-this.age;
- // if(num==0)
- // return this.name.compareTo(o.name);
- // return num;
- // }
- public String toString(){
- return this.name+":"+this.age;
- }
- }
-
复制代码 |