黑马程序员-java之TreeSet,Comparable,Comparator
来源:互联网 发布:网页源码 编辑:程序博客网 时间:2024/05/20 06:05
-------------android培训、java培训、---------------
TreeSet可以对元素进行排序,缺省是按照自然排序进行排列,即元素必须实现Comparable接口,或者创建TreeSet对象的时候必须指定一个比较器
且元素唯一。
排序的两种方式:
方式一:让元素自身具备比较性,即让元素类实现Comparable接口,覆写compareTo(),这种排序方式是默认排序,也叫自然排序
方式二:当元素自身不具备比较性时,或者所具备的比较性不是实际需要的,这是就需要让集合TreeSet具备比较性,即在创建集合对象的时候指定比较器
当两种都存在时候,用比较器
原理:二叉树结构(左叉值小于右叉值),新元素大于原有元素,则放在叉的右边,小于则放在左边,等于则不存储
实例:存储自定义对象
代码:
package com.itheima;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
class Student implements Comparable{
private String name;
private int age;
Student(String name,int age){
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 compareTo(Object obj){//先按年龄比较,年龄相同则按名字自然排序
if(!(obj instanceof Student)){
throw new RuntimeException(obj.toString()+"不是学生");
}
Student stu = (Student)obj;
//System.out.println(this.name+"---compareTo---"+stu.name);
if(this.age>stu.age)
return 1;
if(this.age==stu.age){
return this.name.compareTo(stu.name);
}
return -1;
}
}
class myComparator implements Comparator{//先按姓名自然排序,姓名相同则按年龄升序排序
public int compare(Object o1,Object o2){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num==0){
return new Integer(s1.getAge()).compareTo(s2.getAge());
}
return num;
}
}
public class TreeSetDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet ts = new TreeSet();//创建TreeSet对象
TreeSet ts2 = new TreeSet(new myComparator());
ts.add(new Student("zhangsan",18));//添加元素到对象ts
ts.add(new Student("lisi",22));
ts.add(new Student("wangwu",18));
ts.add(new Student("zhaoliu",19));
ts2.add(new Student("zhangsan",18));//添加元素到ts2
ts2.add(new Student("lisi",22));
ts2.add(new Student("wangwu",18));
ts2.add(new Student("zhaoliu",19));
ts2.add(new Student("wangwu",23));
Iterator it = ts.iterator();
Iterator it2 = ts2.iterator();
System.out.println("----------ts中的元素------------");
while(it.hasNext()){
Student stu = (Student)it.next();
System.out.println(stu.getName()+"---"+stu.getAge());
}
System.out.println("----------ts2中的元素------------");
while(it2.hasNext()){
Student stu2 = (Student)it2.next();
System.out.println(stu2.getName()+"---"+stu2.getAge());
}
}
}
- 黑马程序员-java之TreeSet,Comparable,Comparator
- 黑马程序员-Set-HashSet-TreeSet-Comparator-Comparable
- 黑马程序员------TreeSet集合框架存储自定义元素之排序Comparable与Comparator
- 黑马程序员:Comparable vs Comparator
- 黑马程序员-集合框架【TreeSet】、【二叉树】、【Comparable接口】、【Comparator接口】
- Java之Comparable,Comparator
- 黑马程序员一java集合中的排序之comparable和Comparator 的区别
- “黑马程序员"Comparable与Comparator的区别
- 黑马程序员-----Comparable和Comparator的区别
- 黑马程序员----Comparable接口和Comparator接口
- Java之Comparable和Comparator
- java之Comparator与Comparable
- TreeSet类 Comparator与Comparable
- TreeSet类 Comparator与Comparable
- 黑马程序员——java基础笔记:比较器Comparator和Comparable
- Java 7之基础 - Comparable与Comparator
- JAVA排序之Comparable和Comparator
- Java之Comparable与Comparator的区别
- 第十三周 项目一--动物这样叫(2)抽象类
- socketpair初识
- androidの设置字体样式引起应用程序运行时占用内存过大
- Windows XP和Ubuntu14.04双系统的安装
- myeclipse下载网址
- 黑马程序员-java之TreeSet,Comparable,Comparator
- system v消息队列demo(未编译)
- TT:Number overflow for TO_CHAR function -- file "numberAPI.c"
- IOS开发之__bridge,__bridge_transfer和__bridge_retained
- mysql常用操作命令
- hdu 5254
- redis添加开机启动
- 将USB的默认连接方式改为MTP
- Android Fragment 你应该知道的一切