- import java.util.*;
- class Student implements Comparable<Student>
- {
- private String name;
- private int age;
- public void setName(String name)
- {
- this.name = name;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public int compareTo(Student stu)
- {
- int num = new Integer(this.age).compareTo(stu.age);
- if (num==0)
- return this.name.compareTo(stu.name);
- return num;
- }
- public int hashCode()
- {
- return name.hashCode()+age*39;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("ss");
- Student stu = (Student)obj;
- return this.name.equals(stu.name)&&this.age==stu.age;
- }
- }
- class TreeMapTest
- {
- public static void main(String[] args)
- {
- HashMap<Student,String> hm = new HashMap<Student,String>();
- hm.put(getStu("zhangsan",12),"zhejiang");
- }
- public static Student getStu(String name, int age)
- {
- Student stu = new Student();
- stu.setName(name);
- stu.setAge(age);
- return stu;
- }
- }
复制代码 还没写完的代码,在Windows下可以编译的,Ubuntu下就不行了,原来还有一段代码是StringBuffer的,也编译不通过,我就把Buffer改成Builder,就通过了 |