A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 执剑、砍人 中级黑马   /  2015-6-15 07:02  /  289 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast.compare;
//标准学生类
public class Student implements Comparable<Student> {
        private String name;
        private int age;
        public Student() {
                super();
        }
        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;
        }
       
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + age;
                result = prime * result + ((name == null) ? 0 : name.hashCode());
                return result;
        }
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                Student other = (Student) obj;
                if (age != other.age)
                        return false;
                if (name == null) {
                        if (other.name != null)
                                return false;
                } else if (!name.equals(other.name))
                        return false;
                return true;
        }
        public String toString() {
                return "Student [name=" + name + ", age=" + age + "]";
        }
        public int compareTo(Student s) {
                int num = this.getAge() - s.getAge();
                return num;
        }
}


package cn.itcast.compare;

import java.util.Iterator;
import java.util.TreeSet;

import cn.itcast.mapergodic.Student;
//测试类
public class TreeSetDemo {
        public static void main(String[] args) {
                /**
                 * 定义一个TreeSet对象,存储自定义对象Student。 按照姓名长度的大小决定存储的顺序,
                 * 从长到短排序,如果长度一样,年龄小的在前面
                 */
                //定义一个集合对象,储存元素
                TreeSet<Student> ts = new TreeSet<Student>();
               
                //定义元素
                Student students1 = new Student("zhangsan",34);
                Student students2 = new Student("wangwu",23);
                Student students3 = new Student("zhaoliu",33);
                Student students4 = new Student("niubi",22);
               
                //添加元素
                ts.add(students1);
                ts.add(students2);
                ts.add(students3);
                ts.add(students4);
               
                //遍历
                Iterator<Student> it = ts.iterator();
                while (it.hasNext()) {
                        Student students = it.next();
                        System.out.println(students);
                }
               
        }       
               
        }
}

1 个回复

倒序浏览
这个是类型转换错误,可能是类型做强制转换时出现的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马