黑马程序员技术交流社区
标题:
关于TreesSet
[打印本页]
作者:
孙铭泽
时间:
2012-8-20 22:24
标题:
关于TreesSet
如何保证元素的有序 呢?
treesSet的理解。
作者:
黄敏
时间:
2012-8-20 23:14
本帖最后由 黄敏 于 2012-8-20 23:16 编辑
|--TreeSet:可以对Set集合中的元素进行排序。
底层数据结构是二叉树
保证数据的唯一性是通过compareTo方法return 0 确认的
TreeSet排序的第一种方式:让元素自身具备比较性。
元素需要实现comparable接口,覆盖compareTo方法
这种方式也叫做元素的自然排序,或者叫做默认排序。
TreeSet排序的第二种方式。
当元素自身不具备比较性时,或者具备的比较性不是所有需要的
这是就是需要让集合具备比较性。
在集合初始化时,就有了比较方式。
定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。
当两种排序都村咋时,以比较器为主
如下演示代码:
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02",22));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
ts.add(new Student("lisi08",19));
//ts.add(new Student("lisi007",20));
//ts.add(new Student("lisi01",40));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
}
class Student implements Comparable//该接口强制让学生具备比较性。
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object obj)
{
//return 0;
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
System.out.println(this.name+"....compareto....."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
{
return this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
演示2
import java.util.*;
class Student implements Comparable
//该接口强制让学生具备比较性。
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object obj)
{
//return 0;
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
//System.out.println(this.name+"....compareto....."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
{
return this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class TreeSetDemo2
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02",22));
ts.add(new Student("lisi02",21));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
ts.add(new Student("lisi06",18));
ts.add(new Student("lisi06",18));
ts.add(new Student("lisi007",29));
//ts.add(new Student("lisi007",20));
//ts.add(new Student("lisi01",40));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
}
class MyCompare implements Comparator
//实现Comparator接口,自定义比较器,
{
public int compare(Object o1,Object o2)
//覆写Comparator中的compare方法,实现自定义比较
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num==0)
{
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
}
return num;
}
}
作者:
吴小东
时间:
2012-8-21 17:59
要使TreeSet 集合具有可比性,有两种方法:
一种是可以在定义TreeSet 的时候就用它的构造方法
TreeSet ts = new TreeSet(Comparator<? super E> comparator) ; 就是为TreeSet 自定义一个比较器作为参数传进去
那么我们就需要定义一个比较器的类实现 Comparator接口
class myCompare implements Comparator
{
public int compare(Object obj1,Object obj2) //这里要复写其中compare方法
{
int num = 0;
if (!(obj1 instanceof Person)&&!(obj2 instanceof Person))
{
throw new RuntimeException("不是合法类型!");
}
Person p1 = (Person)obj1;
Person p2 = (Person)obj2;
if (p1.getName()!=p2.getName())
{
num = p1.getName().compareTo(p2.getName());
}
if (p1.getName()==p2.getName())
{
num = (new Integer(p1.getAge())).compareTo(new Integer(p2.getAge()));
}
return num;
}
}
复制代码
另外一个一个方法就是让类自身具有可比性,例如定义一个Person类 继承 Comparable
class Person implements Comparable
{
public int compareTo(Object obj) //复写其中的 compareTo方法
{
if (!(obj instanceof Person))
{
throw new RuntimeException("参数类型不匹配!");
}
int num = 0;
Person p1 = (Person)obj;
if (p1.getName()!=this.getName())
{
num = this.getName().compareTo(p1.getName());
}
if (p1.getName()==this.getName())
{
num = (new Integer(this.getAge())).compareTo(new Integer(p1.getAge()));
}
return num;
}
}
复制代码
这样元素自身具有可比性,在往TreeSet 中存放的时候,就会按你定义的顺序排列
特别提醒一下 其中几个方法的名字 继承Comparable -->> compareTo() 实现Comparator-->> compare() 注意 are 和 ara 经常搞混乱
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2