Comparator
假设Hero有三个属性 name,hp,damage
一个集合中放存放10个Hero,通过Collections.sort对这10个进行排序
那么到底是hp小的放前面?还是damage小的放前面?Collections.sort也无法确定
所以要指定到底按照哪种属性进行排序
这里就需要提供一个Comparator给定如何进行两个对象之间的大小比较
Hero代码
package charactor;public classHero {publicString name;public floathp;public intdamage;publicHero() {
}publicHero(String name) {this.name =name;
}publicString toString() {return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\r\n";
}public Hero(String name, int hp, intdamage) {this.name =name;this.hp =hp;this.damage =damage;
}
}
TestCollection代码
package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import charactor.Hero;public classTestCollection {public static voidmain(String[] args) {
Random r=newRandom();
List heros = new ArrayList();for (int i = 0; i < 10; i++) {//通过随机值实例化hero的hp和damage
heros.add(new Hero("hero"+ i, r.nextInt(100), r.nextInt(100)));
}
System.out.println("初始化后的集合:");
System.out.println(heros);//直接调用sort会出现编译错误,因为Hero有各种属性//到底按照哪种属性进行比较,Collections也不知道,不确定,所以没法排//Collections.sort(heros);//引入Comparator,指定比较的算法
Comparator c = new Comparator() {
@Overridepublic intcompare(Hero h1, Hero h2) {//按照hp进行排序
if(h1.hp>=h2.hp)return 1; //正数表示h1比h2要大
else
return -1;
}
};
Collections.sort(heros,c);
System.out.println("按照血量排序后的集合:");
System.out.println(heros);
}
}
Comparable
使Hero类实现Comparable接口
在类里面提供比较算法
Collections.sort就有足够的信息进行排序了,也无需额外提供比较器Comparator
Hero代码
package charactor;public class Hero implements Comparable{publicString name;public floathp;public intdamage;publicHero(){
}publicHero(String name) {this.name =name;
}//初始化name,hp,damage的构造方法
public Hero(String name,float hp, intdamage) {this.name =name;this.hp =hp;this.damage =damage;
}
@Overridepublic intcompareTo(Hero anotherHero) {if(damage
return -1;
}
@OverridepublicString toString() {return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\r\n";
}
}
TestCollection代码
package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import charactor.Hero;public classTestCollection {public static voidmain(String[] args) {
Random r=newRandom();
List heros = new ArrayList();for (int i = 0; i < 10; i++) {//通过随机值实例化hero的hp和damage
heros.add(new Hero("hero"+ i, r.nextInt(100), r.nextInt(100)));
}
System.out.println("初始化后的集合");
System.out.println(heros);//Hero类实现了接口Comparable,即自带比较信息。//Collections直接进行排序,无需额外的Comparator
Collections.sort(heros);
System.out.println("按照伤害高低排序后的集合");
System.out.println(heros);
}
}
自定义顺序的TreeSet
默认情况下,TreeSet中的数据是从小到大排序的,不过TreeSet的构造方法支持传入一个Comparator
public TreeSet(Comparator comparator)
通过这个构造方法创建一个TreeSet,使得其中的的数字是倒排序的
TreeSetDemo代码(实现倒序)
package Test.testtest;
import java.util.Comparator;
import java.util.TreeSet;/**
* @Auther: 李景然
* @Date: 2018/5/25 14:43
* @Description:*/
public classTreeSetDemo {public static voidmain(String[] args) {
TreeSet treeSet=new TreeSet<>(new Comparator() {
@Overridepublic intcompare(Integer o1, Integer o2) {return -Integer.compare(o1,o2);//实现倒序
}
});for (int i=0;i<100;i++){double d=Math.random()*100;
treeSet.add(Integer.valueOf((int)d));
}
System.out.println(treeSet);
}
}
运行结果: