package cn.itcast.p.bean;
import java.util.ArrayList;
import java.util.Iterator;
public class Person /*extends Object*/ implements Comparable {
public static void main(String []augs){
// Person p = new Person("p1",3);
ArrayList al = new ArrayList();
al.add(new Person("p1",3));
al.add(new Person("p2",2));
al.add(new Person("p3",3));
al.add(new Person("p4",4));
al.add(new Person("p5",5));
Iterator it = al.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println(p.getName() + " " + p.getAge());
}
}
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
// System.out.println(this+".......hashCode");
return name.hashCode()+age*37;
// return 100;
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
//健壮性判断
if(!(obj instanceof Person))
throw new ClassCastException("类型错误");
// System.out.println(this+"....equals....."+obj);
Person p = (Person)obj;
return this.name.equals(p.name) && this.age == p.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 String toString(){
return name+":"+age;
}
//在TreeSet里边进行排序比较,需要重写这个方法
@Override
public int compareTo(Object o) {
//要做健壮性判断
if(!(o instanceof Person)){
throw new ClassCastException("类型不匹配异常");
}
Person p = (Person)o;
int temp = this.age-p.age;
return temp==0?this.name.compareTo(p.name):temp;
// int temp = this.name.compareTo(p.name);
// return temp==0?this.age-p.age:temp;
/*
if(this.age>p.age)
return 1;
if(this.age<p.age)
return -1;
else{
return this.name.compareTo(p.name);
}
*/
}
}
package cn.itcast.p5.treeset.demo;
import java.util.Iterator;
import java.util.TreeSet;
import cn.itcast.p.bean.Person;
import cn.itcast.p5.comparator.ComparatorByName;
public class TreeSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
TreeSet ts = new TreeSet(new ComparatorByName());
/*
* ��Person����������д�С���������
*
*/
ts.add(new Person("zhangsan",28));
ts.add(new Person("lisi",21));
ts.add(new Person("zhouqi",29));
ts.add(new Person("zhaoliu",25));
ts.add(new Person("wangu",24));
Iterator it = ts.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getName()+":"+p.getAge());
}
}
/**
*
*/
public static void demo1() {
TreeSet ts = new TreeSet();
ts.add("abc");
ts.add("zaa");
ts.add("aa");
ts.add("nba");
ts.add("cba");
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
package cn.itcast.p5.comparator;
import java.util.Comparator;
import cn.itcast.p.bean.Person;
/**
* ������һ�����Person���name��������ıȽ�����
*/
public class ComparatorByName implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Person p1 = (Person)o1;
Person p2 = (Person)o2;
//比较姓名(compareTO,就是按字典比较顺序比较字符串大小,也就是比较名字字符串的大小)
int temp = p1.getName().compareTo(p2.getName());
//如果名字相同,则继续比较年龄
return temp==0?p1.getAge()-p2.getAge(): temp;
// return 1;//����
}
}
package cn.itcast.p5.comparator;
import java.util.Comparator;
public class ComparatorByLength implements Comparator {
@Override
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
//比较对象(字符串)的长度,也就是有多少个
int temp = s1.length()-s2.length();
System.out.println("s1: " + s1 + " " + "s2: "+ s2 + " ");
//当长度相等的时候,再比较内容,也就是哪个大
return temp==0? s1.compareTo(s2): temp;
}
}
package cn.itcast.p5.treeset.test;
import java.util.Iterator;
import java.util.TreeSet;
import cn.itcast.p5.comparator.ComparatorByLength;
/*
* ���ַ���г�������
*
* "20 18 -1 89 2 67"
*/
public class TreeSetTest {
/**
* @param args
*/
public static void main(String[] args) {
TreeSet ts = new TreeSet(new ComparatorByLength());
ts.add("aaaaa");
ts.add("zz");
ts.add("nbaq");
ts.add("cba");
ts.add("abc");
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
TreeSet是按自定义或者默认比较器进行排序的元素唯一的集合。
使用TreeSet
|--TreeSet:可以对Set集合中的元素进行排序。是不同步的。
判断元素唯一性的方式:就是根据比较方法的返回结果是否是0,是0,就是相同元素,不存。
TreeSet对元素进行排序的方式一:
让元素自身具备比较功能,元就需要实现Comparable接口。覆盖compareTo方法。
如果不要按照对象中具备的自然顺序进行排序。如果对象中不具备自然顺序。怎么办?
可以使用TreeSet集合第二种排序方式二:
让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。将该类对象作为参数传递给TreeSet集合的构造函数。