一 概述

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. ActiveMQ消息队列的搭建和使用

    一.安装ActiveMQ(部署在centos7) 1.ActiveMQ官网下载地址:http://activemq.apache.org/download.html 2.解压安装包:tar xvzf  ...

  2. IntelliJ IDEA 把Maven项目导出可执行jar包

    2017年04月05日 14:05:08 waterimelon 阅读数:1574 标签: intellij ideamaven 更多 个人分类: idea   第一步  第二步  第三步 

  3. mysql数据库怎么使用,mysql的使用方法

    https://jingyan.baidu.com/article/5d368d1ec069c13f61c05742.html 数据库的开启与关闭: https://blog.csdn.net/u01 ...

  4. C++_标准模板库STL概念介绍4-算法

    STL包含很多处理容器的非成员函数: sort() copy() find() random_shuffle() set_union() set_intersection() set_differen ...

  5. c# post get

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Tensorlflow-神经网络解决非线性回归问题

    import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt #使用numpy生成200个随机点,范围从-0.5到0 ...

  7. UVA - 11922 区间反转+拼接 可持久化Treap

    题意:一开始给出一个序列\(1,2...n\),然后\(m\)次操作,每次把\([l,r]\)翻转并且拼接到序列的后面,求最后形成的序列 打个pushdown标记就好 #include<iost ...

  8. Python 实现 ZoomEye API SDK

    版权声明:未经作者授权,禁止转载! ZoomEye想必大家都很熟悉,自从官方开放了API,网上各种版本的SDK乱飞.今天我也来发一个自己写的. 首先我们从https://github.com/SEC0 ...

  9. VBS常用脚本及其解说一览

    取得本机IP strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strCo ...

  10. C++ GUI Qt4编程(08)-3.2spreadsheet-resource

    1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...