一 概述

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. Jenkins项目部署使用教程-----03节点添加

    1)添加节点 系统管理——>管理节点——>新建节点 进入配置界面 点ok进入配置界面配置,——>高级 点击save保存,进入节点,点击Launch agent使得jenkins服务器 ...

  2. linux下使用文件IO监听GPIO中断

    完整的程序如下: #include<stdlib.h> #include<stdio.h> #include<string.h> #include<unist ...

  3. 带权并查集 - How Many Answers Are Wrong

    思路: 带权并查集+向量偏移 #include <iostream> using namespace std; int n, m; ]; ]; // 到根节点的距离 ; void init ...

  4. 洛谷P2146 [NOI2015]软件包管理器

    https://www.luogu.org/problemnew/show/P2146 传送门 简单的树链剖分......维护下当前安装了多少个包......修改后查询下就行了......附上极其丑陋 ...

  5. 洛谷 P4859 && BZOJ3622: 已经没有什么好害怕的了

    题目描述 给出 \(n\) 个数 \(a_i\)​ ,以及 \(n\) 个数 \(b_i\)​ ,要求两两配对使得 \(a>b\) 的对数减去 \(a<b\) 的对数等于 \(k\) . ...

  6. LightOJ - 1140 统计0的数位 数位DP

    注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...

  7. django contenttype 表应用

    Django contenttypes 应用 contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. 每当我们创建 ...

  8. kindeditor<=4.1.5文件上传漏洞

    最近发现很多网页篡改与暗链都是利用kindeditor编辑器,于是搜了一下kindeditor的漏洞,发现低于4.1.5版本的存在文件上传的漏洞,可以上传txt,html后缀的文档,许多恶意的文档貌似 ...

  9. 完全原生javascript简约日历插件,js、html

    效果图: 效果如图所示,尽管看上去并不是很美观,但是,基本上的功能还是已经完成了,码了一天多的时间,权当做复习一下js吧. 整个做下来差不多码了500多行代码~其实只是很多的样式也包括了在其中了,虽然 ...

  10. (转) CentOS 7添加开机启动服务/脚本

    CentOS 7添加开机启动服务/脚本 原文:http://blog.csdn.net/wang123459/article/details/79063703 一.添加开机自启服务 在CentOS 7 ...