62,632
社区成员
发帖
与我相关
我的任务
分享
import java.util.Set;
import java.util.TreeSet;
class Person implements Comparable<Person>{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+this.name+" 年龄:"+this.age;
}
public int compareTo(Person per){
if(this.age>per.age){
return 1;
}else if(this.age==per.age){
return 0;
}else{
return -1;
}
}
}
public class SetDemo03{
public static void main(String args[]){
Set<Person> allSet=new TreeSet<Person>();
allSet.add(new Person("张三",22));
allSet.add(new Person("李四",23));
allSet.add(new Person("王五",21));
allSet.add(new Person("王五",21));
allSet.add(new Person("王五",21));
allSet.add(new Person("赵六",32));
allSet.add(new Person("孙七",11));
System.out.println(allSet);
}
}
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
其中,m是TreeMap的一个实例,再看一下TreeMap中的put方法源码
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
// TBD:
// 5045147: (coll) Adding null to an empty TreeSet should
// throw NullPointerException
//
// compare(key, key); // type check
root = new Entry<K,V>(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
Entry<K,V> e = new Entry<K,V>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
其中Comparable<? super K> k = (Comparable<? super K>) key;就要求K类型的实例要实现Comparable接口,在cmp = k.compareTo(t.key);就会调用自己实现的compareTo()方法,具体怎么进行比较,就是由你自己实现的。
上面的源码都是JDK6版本的package com.test;
import java.util.Set;
import java.util.TreeSet;
public class Test5 {
public static void main(String[] args) {
Set<Person> allSet =new TreeSet<Person>();
allSet.add(new Person("zhangsan",20));
allSet.add(new Person("lsi",19));
allSet.add(new Person("wangwu",39));
allSet.add(new Person("zhaoliu",23));
allSet.add(new Person("caidsa",21));
System.out.println(allSet.toString());
}
}
class Person implements Comparable<Person>
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return "姓名:"+this.name+" 年龄:"+this.age;
}
@Override
public int compareTo(Person per) {
System.out.println("正在调用compareTo方法");
if(this.age>per.age)
{
return 1;
}
else if(this.age==per.age)
{
return 0;
}else
{
return -1;
}
}
}
[quote=引用 1 楼 suciver 的回复:] 这种东西楼主看TreeSet的源码就知道了.... TreeSet的底层实现其实是TreeMap在每次add的时候也就是调用TreeMap的put方法这时如果有外部比较器Comparator的实现就会调用外部的Comparator的比较方法,如果没有就会调用实现Comparable接口的类的compareTo方法
你自己定义了一个排序的方案,那么Set集合背部就会调用compareTo方法,就跟你打印会自动调用toString方法一样自然 ,不懂可以追问
没看见你的compareTo方法?要自己写的
public int compareTo(Person per){
if(this.age>per.age){
return 1;
}else if(this.age==per.age){
return 0;
}else{
return -1;
}
}
如果我没看错的话这个,应该是会和sort()函数配合使用的import java.util.Arrays;
class AlohaPerson implements Comparable<AlohaPerson> {
private String name;
AlohaPerson(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(AlohaPerson o) {
return this.name.compareTo(o.name);
}
}
public class Aloha {
public static void sort(AlohaPerson[] persons) {
// 用选择排序法排序
for (int i = 0; i < persons.length - 1; ++i) {
int k = i;
for (int j = k; j < persons.length; ++j) {
// 排序的时候在比较两个对象时用compareTo方法.
if (persons[j].compareTo(persons[k]) < 0) {
k = j;
}
}
if (k != i) {
AlohaPerson temp = persons[k];
persons[k] = persons[i];
persons[i] = temp;
}
}
}
public static void main(String[] args) {
AlohaPerson[] persons = {
new AlohaPerson("Bob"),
new AlohaPerson("John"),
new AlohaPerson("Smith"),
new AlohaPerson("Alpha"),
new AlohaPerson("Beta")
};
System.out.println(Arrays.toString(persons));
sort(persons);
System.out.println(Arrays.toString(persons));
}
}
输出
[Bob, John, Smith, Alpha, Beta]
[Alpha, Beta, Bob, John, Smith]
这种东西楼主看TreeSet的源码就知道了.... TreeSet的底层实现其实是TreeMap在每次add的时候也就是调用TreeMap的put方法这时如果有外部比较器Comparator的实现就会调用外部的Comparator的比较方法,如果没有就会调用实现Comparable接口的类的compareTo方法