TreeSet自然排序

package com.newedu.jb.day18.set;


public class Student implements Comparable<Student>{

private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
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 String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
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;
}

/**
* 只有实现了该方法,才能让Student的所有对象,具有自然排序的属性。


*/
@Override
public int compareTo(Student o) {
/**
* 两个对象,谁大,谁小,自己定

* 例如:下面是一种方案:
*  只比较学生类的对象的年龄;
*  对象的年龄大了,我们就认为该对象。
*  
*  
*  另外一种方案:
*  如果比较两个对象的大小,常见的方案,逐个比较成员变量;
*  如果常用变量相等了,才需要进一步比较第二个成员变量。
*/
//从小到大
// return this.age<o.age? -1 :(this.age==o.age?0:1);

//从大到小
// return this.age<o.age? 1 :(this.age==o.age?0:-1);

// "aa","b"
int num = this.name.compareTo(o.name);

//如果名字已经比较出来了大小,就不再需要进一步的比较年龄了。
//否则 名字 相等的情况下,才需要进一步比较年龄
// 从大到小
return num != 0?num:(this.age<o.age? 1 :(this.age==o.age?0:-1));
}



}

package com.newedu.jb.day18.set;


import java.util.TreeSet;


/**
 * TreeSet的特点:
 * 是Set接口的实现类
 * 
 * 1:内部依赖 红黑树(自平衡二叉树) 默认对元素进行自然排序
 * 2:元素不允许重复
 * 3:不允许null值的出现。
 * 4:存取有序吗? 存取无效
 * 5:线程不安全,效率高
 * 
 * 举例说明:
 * 
 * 存取Integer类型,并且遍历
 * 
 * 
 * 什么叫自然排序?
 * 
 * Comparable接口,此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序
 * 
 *    只有一个方法 int compareTo(T t)
 *     该方法的返回值,有三种可能:
 *     负整数:小于指定对象
 *        零:等于指定对象
 *     正整数:大于指定对象
 *     
 *     
 *  再举一个自定义类型的存储(Student),并且遍历
 *  
 *  
 *  构造方法:
 *  TreeSet() 
          构造一个新的空 set,该 set 根据其元素的自然顺序进行排序
          
    TreeSet(Comparator<? super E> comparator) 
          构造一个新的空 TreeSet,它根据指定比较器进行排序。 自定义排序
 */
public class TreeSetDemo1 {


public static void main(String[] args) {


//第一步:创建集合对象
TreeSet<Integer> tset = new TreeSet<Integer>();


//第二步:创建集合元素

Integer i1 = 3;
Integer i2 = 2;
Integer i3 = 7;
Integer i4 = 5;
Integer i5 = 9;
Integer i6 = 4;
Integer i7 = 5;//重复值
Integer i8 = null;

//第三步:向集合中添加元素

tset.add(i1);
tset.add(i2);
tset.add(i3);
tset.add(i4);
tset.add(i5);
tset.add(i6);
tset.add(i7);
// tset.add(i8);//null值不允许
// return (x < y) ? -1 : ((x == y) ? 0 : 1);

//第四步:遍历集合
for(Integer item :tset){
System.out.print(item);
System.out.print(",");
}

System.out.println("=========自定义类型Student===============");

/**
* Exception in thread "main" java.lang.ClassCastException: com.newedu.jb.day18.set.Student cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1294)
at java.util.TreeMap.put(TreeMap.java:538)
at java.util.TreeSet.add(TreeSet.java:255)
at com.newedu.jb.day18.set.TreeSetDemo1.main(TreeSetDemo1.java:80)


*/
TreeSet<Student> tset1 = new TreeSet<Student>();

Student stu1 = new Student("jerry",20);

Student stu2 = new Student("susan",19);

Student stu3 = new Student("peter",18);
Student stu4 = new Student("jerry",18);

tset1.add(stu1);
tset1.add(stu2);
tset1.add(stu3);
tset1.add(stu4);

for(Student stu : tset1){
System.out.println("集合元素:"+stu);
}

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值