步骤1:Comparator
步骤2:Comparable
步骤3:练习-自定义顺序的TreeSet
步骤4:答案-自定义顺序的TreeSet
步骤5:练习-Comparable
步骤6:答案-Comparable
步骤 1 : Comparator
假设Hero有三个属性 name,hp,damage
一个集合中放存放10个Hero,通过Collections.sort对这10个进行排序
那么到底是hp小的放前面?还是damage小的放前面?Collections.sort也无法确定
所以要指定到底按照哪种属性进行排序
这里就需要提供一个Comparator给定如何进行两个对象之间的大小比较
![Comparator](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9zdGVwaW1hZ2V3bS5ob3cyai5jbi84MjgucG5n?x-oss-process=image/format,png)
package charactor;
public class Hero {
public String name;
public float hp;
public int damage;
public Hero() {
}
public Hero(String name) {
this .name = name;
}
public String toString() {
return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\r\n" ;
}
public Hero(String name, int hp, int damage) {
this .name = name;
this .hp = hp;
this .damage = damage;
}
}
|
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 class TestCollection {
public static void main(String[] args) {
Random r = new Random();
List<Hero> heros = new ArrayList<Hero>();
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<Hero> c = new Comparator<Hero>() {
@Override
public int compare(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);
}
}
|
步骤 2 : Comparable
使Hero类实现Comparable接口
在类里面提供比较算法
Collections.sort就有足够的信息进行排序了,也无需额外提供比较器Comparator
注: 如果返回-1, 就表示当前的更小,否则就是更大
![Comparable](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9zdGVwaW1hZ2V3bS5ob3cyai5jbi84MjkucG5n?x-oss-process=image/format,png)
package charactor;
public class Hero implements Comparable<Hero>{
public String name;
public float hp;
public int damage;
public Hero(){
}
public Hero(String name) {
this .name =name;
}
//初始化name,hp,damage的构造方法
public Hero(String name, float hp, int damage) {
this .name =name;
this .hp = hp;
this .damage = damage;
}
@Override
public int compareTo(Hero anotherHero) {
if (damage<anotherHero.damage)
return 1 ;
else
return - 1 ;
}
@Override
public String toString() {
return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\r\n" ;
}
}
|
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 class TestCollection {
public static void main(String[] args) {
Random r = new Random();
List<Hero> heros = new ArrayList<Hero>();
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);
}
}
|
更多内容,点击了解: https://how2j.cn/k/collection/collection-comparator-comparable/693.html