一 概述

1.Comparable与Comparator使用背景

数值型数据(byte int short long float double)天生可对比大小,可排序,String实现了Comparable接口也可以对比大小与排序,而自定义类多种多样,没有一个共有的可以用作排序的指标,因此需要在自定义类中手动建立对比的方法,出于这个目的,java提供了两个接口Comparable与Comparator。

2.集合排序

Collections.sort()底层排序依靠的是Arrays.sort(),而Arrays.sort()排序时采用的是冒泡法。

二 Comparable

需要对比大小的对象可以实现Comparable接口,实现其中的抽象方法,该抽象方法用来设定比较的方式。下面以一个示例进行说明:

1.实体类

package com.javase.collections.comparable;

public class Student implements Comparable<Student> {

    private String name;
private int score; public Student() {
super();
} public Student(String name, int score) {
super();
this.name = name;
this.score = score;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} @Override
public int compareTo(Student stu) {
return this.score - stu.score;// 操作对象减去参数对象,升序排列,反之降序。
} }

在compareTo()方法中,以属性score为排序指标,采用“this.score-stu.score”,最终结果以升序排列,反之降序。

2.测试类

package com.javase.collections.comparable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import org.junit.Test; public class ComparableTest { @Test
public void testComparable() {
List<Student> stus = new ArrayList<Student>();
Student zhangsan = new Student("zhangsan", 100);
Student lisi = new Student("lisi", 90);
Student wanger = new Student("wanger", 95);
stus.add(zhangsan);
stus.add(lisi);
stus.add(wanger);
System.out.println("排序前");
for (Student x : stus) {
System.out.println(x.getName() + "::" + x.getScore());
}
System.out.println("排序后");
Collections.sort(stus);
for (Student x : stus) {
System.out.println(x.getName() + "::" + x.getScore());
}
} }

输出:

三 Comparator

如果一个类在创建时未实现Comparable接口,希望在不修改源码的情况下对其对象进行排序,可以在调用排序方法时实现Comparator比较器接口,指定排序方法。下面以一个示例进行说明:

1.实体类

package com.javase.collections.comparator;

public class Student {
private String name;
private int score; public Student() {
super();
} public Student(String name, int score) {
super();
this.name = name;
this.score = score;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} }

2.测试类

package com.javase.collections.comparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import org.junit.Test; public class ComparatorTest { @Test
public void test() {
List<Student> stus = new ArrayList<Student>();
Student zhangsan = new Student("zhangsan", 100);
Student lisi = new Student("lisi", 90);
Student wanger = new Student("wanger", 95);
stus.add(zhangsan);
stus.add(lisi);
stus.add(wanger);
System.out.println("排序前");
for (Student x : stus) {
System.out.println(x.getName() + "::" + x.getScore());
}
System.out.println("-----------------------");
Collections.sort(stus, new Comparator<Student>() {
@Override
public int compare(Student stu01, Student stu02) {
// return stu01.getScore() - stu02.getScore();//升序
return stu02.getScore() - stu01.getScore();// 降序
}
}); System.out.println("排序后");
for (Student x : stus) {
System.out.println(x.getName() + "::" + x.getScore());
}
} }

在compare(Student stu01, Student stu02)方法中,以属性score为排序指标,采用“stu01.score-stu02.score”,最终结果升序排列,反之降序。

输出:

参考:

http://www.tiantianbianma.com/java-comparable-comparator.html/

对象大小对比之Comparable与Comparator的更多相关文章

  1. 对象比较器:Comparable和Comparator

    在进行对象数组排序的过程中需要使用到比较器,比较器有两个:Comparable和Comparator ①.java.lang.Comparable:是在类定义是时候默认实现好的接口,里面提供有一个co ...

  2. Map集合的遍历方式以及TreeMap集合保存自定义对象实现比较的Comparable和Comparator两种方式

    Map集合的特点 1.Map集合中保存的都是键值对,键和值是一一对应的 2.一个映射不能包含重复的值 3.每个键最多只能映射到一个值上 Map接口和Collection接口的不同 Map是双列集合的根 ...

  3. Java ArrayList中对象的排序 (Comparable VS Comparator)

    我们通常使用Collections.sort()方法来对一个简单的数据列表排序.但是当ArrayList是由自定义对象组成的,就需要使用comparable或者comparator接口了.在使用这两者 ...

  4. Java中Comparable与Comparator的区别

    相同 Comparable和Comparator都是用来实现对象的比较.排序 要想对象比较.排序,都需要实现Comparable或Comparator接口 Comparable和Comparator都 ...

  5. Java中Comparable和Comparator接口区别分析

    Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...

  6. Comparable和Comparator的区别

    Comparable Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较 ...

  7. 黑马----JAVA比较器:Comparable和Comparator

    黑马程序员:Java培训.Android培训.iOS培训..Net培训 一.Comparable接口 1.public interface Comparable{ public int compare ...

  8. Java中Comparable和Comparator区别小结

    一.Comparable简介 Comparable是排序接口.若一个类实现了Comparable接口,就意味着该类支持排序.实现了Comparable接口的类的对象的列表或数组可以通过Collecti ...

  9. Java 中 Comparable 和 Comparator 比较

    Java 中 Comparable 和 Comparator 比较 目录: Comparable Comparator Comparable 和 Comparator比较 第二个例子 之 Compar ...

随机推荐

  1. win10+anaconda环境下pyqt5+qt tools+eric6.18安装及汉化过程

    最近需要用python编写一个小程序的界面,选择了pyqt5+eric6的配套组合,安装过程中遇到一些坑,特此记录.参考书籍是电子工业出版社的<PyQt5快速开发与实战>. 因为我使用an ...

  2. php 其他格式数据与数组互转

    class otherArr { private $char="UTF-8"; private $cvs_fege=","; // cvs 分割符 /**数组 ...

  3. docker image rm ubuntu 失败

    [root@hadoop14 ~]# docker image rm ubuntu Failed to remove image (ubuntu:v2): Error response from da ...

  4. python-sort()/sorted()比较

    Sorting Lists sorted(iterable,key=None,reverse=False),does not mutate list, must assign result to a ...

  5. css 之 BFC

    1,定义 BFC为块级格式化上下文,也就是一块区域内的封闭空间,里面元素无论怎么样,都不会影响外部元素. 2,触发条件 html 根元素 display的值为 inline-block.table-c ...

  6. ansys 14.0

    张建伟等.2012.12 人民邮电 一天一本书,这种感觉 真是爽啊. 但我觉得什么也没学到. 话说回来,此authors 确实书写得比较一般,实例不如百度文档上的WORD实例,理论不如我看的那本CFD ...

  7. PHP操作Access数据库

    ADO是一项微软的技术,ADO指ActiveX数据对象(ActiveX Data Objects). 链接数据库 <?php header("Content-Type:text/htm ...

  8. spring4.x hibernate4.x 整合 ehcache 注解 annotate

    [From] http://my.oschina.net/alexgaoyh/blog/348188

  9. 认识CSS中css的三大特性:层叠性、继承性以及优先级

    前端之HTML.CSS(四) CSS CSS三大特性 层叠性:多种样式的叠加,一个属性通过两个选择器设置在同一个元素上,后一个样式会把前一个样式层叠(覆盖).层叠性的两种情况:第一种样式冲突时,后样式 ...

  10. 迪米特法则(Law of Demeter)LoD

    using System; using System.Collections.Generic; using System.Text; namespace LawOfDemeter { //也叫Leas ...