暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

从模特身上区分Comparable和Comparator

磊哥谈技术 2019-11-06
302

Comparable一般实现自然比较,同类对象使用的过程中有时候需要比较大小,比方说对象放入有序集合,像我们将对象放入SortedMap时,对象类如果实现了Comparable接口,如果不指定Comparator的话,默认使用Comparable的compareTo方法比较对象大小。


Comparator一般用于比较器比较,有些类可能没有实现Comparable接口,或者找不到合适的自然比较方法,在特定场景下,如果需要比较对象大小的话,可以使用实现了Comparator的比较器进行比较。比较器的比较方法实现可以非常灵活,在不同的场景下,可以实现不同的比较器。


下面我们通过一个模特的例子,来区分Comparable和Comparator。


一群模特站一队的话,为了层次感,我们一般会要求她们按照身高排序。这样我们可以编写模特类,实现Comparable接口,按照身高进行排序。但是单凭身高,我们是不能断定一个模特的综合得分的,比方说,除了身高,我们将三围也加入到模特的评分中,这样我们可以编写一个比较器类实现Comparator。根据身高和三围的评分占比,对模特进行一个综合评比。


实现代码如下所示:

import java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.List;
public class
Test {

public static void main(String[] args) {
List<Belle> belles = new ArrayList<>();
belles.add(Belle.BelleBuilder.newBelleBuilder()
.name("林志玲")
.stature(173)
.bust(86).waist(60).hip(91).build());
belles.add(Belle.BelleBuilder.newBelleBuilder()
.name("刘雯")
.stature(178)
.bust(78).waist(58).hip(85).build());
belles.add(Belle.BelleBuilder.newBelleBuilder()
.name("奚梦瑶")
.stature(177)
.bust(83).waist(60).hip(89).build());
System.out.println("按添加顺序排序");
for
(Belle a : belles)
System.out.println(a);
System.out.println("按身高排序");
Collections.sort(belles);
Collections.reverse(belles);
for
(Belle a : belles)
System.out.println(a);
System.out.println("按综合得分排序");
Collections.sort(belles, Belle.compositeComparator);
Collections.reverse(belles);
for
(Belle a : belles)
System.out.println(a);
}

static class Belle implements Comparable<Belle> {

String name;//姓名
int stature;//身高
int bust;//胸围
int waist;//腰围
int hip;//臀围
private static final int STATURE_RATIO = 30;//身高占比
private static final int BUTST_RATIO = 30;//胸围占比
private static final int WAIST_RATIO = 20;//腰围占比
private static final int HIP_RATIO = 20;//臀围占比
private Belle(BelleBuilder belleBuilder) {
this.name = belleBuilder.name;
this
.stature = belleBuilder.stature;
this
.bust = belleBuilder.bust;
this
.waist = belleBuilder.waist;
this
.hip = belleBuilder.hip;
}

@Override
public int compareTo(Belle anotherBelle) {
return this.stature - anotherBelle.stature;
}

@Override
public String toString() {
return this.name + "[" + this.stature
+ ',' + this.bust
+ ',' + this.waist
+ ',' + this.hip + "]";
}

public static class BelleBuilder {
String name;//姓名
int stature;//身高
int bust;//胸围
int waist;//腰围
int hip;//臀围
public static BelleBuilder newBelleBuilder() {
return new BelleBuilder();
}

public Belle build() {
return new Belle(this);
}

public BelleBuilder name(String name) {
this.name = name;
return this;
}

public BelleBuilder stature(int stature) {
this.stature = stature;
return this;
}

public BelleBuilder bust(int bust) {
this.bust = bust;
return this;
}

public BelleBuilder waist(int waist) {
this.waist = waist;
return this;
}

public BelleBuilder hip(int hip) {
this.hip = hip;
return this;
}
}

public static final Comparator<Belle>
compositeComparator = new CompositeComparator();
private static class
CompositeComparator
implements Comparator<Belle> {
public int compare(Belle belle1, Belle belle2) {
return (belle1.stature - belle2.stature)
* Belle.STATURE_RATIO
+ (belle1.bust - belle2.bust)
* Belle.BUTST_RATIO
+ (belle2.waist - belle1.waist)
* Belle.WAIST_RATIO
+ (belle1.hip - belle2.hip)
* Belle.HIP_RATIO;
}
}
}
}


运行结果如下所示:


文章转载自磊哥谈技术,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论