比较器 comparable与comparator用法
comparable
接口 Comparable<T>
类型参数:T - 可以与此对象进行比较的那些对象的类型
public interface Comparable<T>
此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法。
实现此接口的对象列表(和数组)可以通过 Collections.sort(和 Arrays.sort)进行自动排序。实现此接口的对象可以用作有序映射中的键或有序集合中的元素,无需指定比较器。
负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
compareTo(对象)
if(this. > ) return -1; //高到低排序
例子:学生分数高到低,年龄低到高排序
package com.ij34; /**
* Created by Admin on 2018/3/7.
*/
public class Student implements Comparable<Student>{
private String name;
private int age;
private float score; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public float getScore() {
return score;
} public void setScore(float score) {
this.score = score;
} public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
} @Override
public int compareTo(Student o) {
if (this.score > o.score) return -1;
else if (this.score < o.score) return 1;
else{
if (this.age > o.age) return 1;
else if (this.age < o.age) return -1;
else return 0;
}
}
}
package com.ij34; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; /**
* Created by Admin on 2018/3/7.
*/
public class ComparableStudentTest {
public static void main(String[] args) {
ArrayList<Student> arrays=new ArrayList<Student>();
Student a=new Student("李白",23,88);
Student b=new Student("张三",21,80);
Student c=new Student("李四",22,78);
Student d=new Student("萧峰",24,77);
arrays.add(a);
arrays.add(b);
arrays.add(c);
arrays.add(d);
Collections.sort(arrays,Student::compareTo);
for(Student s:arrays){
System.out.println(s);
}
}
}

comparator
java.util
接口 Comparator<T>
类型参数:T - 此 Comparator 可以比较的对象类型
强行对某个对象 collection 进行整体排序 的比较函数。可以将 Comparator 传递给 sort 方法(如 Collections.sort 或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用 Comparator 来控制某些数据结构(如有序 set或有序映射)的顺序,或者为那些没有自然顺序的对象 collection 提供排序。
| int | compare(T o1, T o2) 比较用来排序的两个参数 |
| boolean | equals(Object obj) 指示某个其他对象是否“等于”此 Comparator |
package com.ij34; /**
* Created by Admin on 2018/3/7.
*/
public class Student{
private String name;
private int age;
private float score; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public float getScore() {
return score;
} public void setScore(float score) {
this.score = score;
} public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
package com.ij34; import java.util.Comparator; /**
* Created by Admin on 2018/3/7.
*/
public class Studentcomparator implements Comparator<Student>{
public int compare(Student o1,Student o2){
if(o1.getScore()>o2.getScore())return -1;
else if(o1.getScore()<o2.getScore()) return 1;
else {
if (o1.getAge()>o2.getAge()) return 1;
else if (o1.getAge()<o2.getAge()) return -1;
else return 0;
}
}
}
package com.ij34; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; /**
* Created by Admin on 2018/3/7.
*/
public class ComparableStudentTest {
public static void main(String[] args) {
ArrayList<Student> arrays=new ArrayList<Student>();
Student a=new Student("李白",23,88);
Student b=new Student("张三",21,80);
Student c=new Student("李四",22,78);
Student d=new Student("萧峰",24,77);
arrays.add(a);
arrays.add(b);
arrays.add(c);
arrays.add(d);
Collections.sort(arrays,new Studentcomparator());
for(Student s:arrays){
System.out.println(s);
}
}
}

比较器 comparable与comparator用法的更多相关文章
- 比较器comparable与comparator的使用
在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...
- 比较器Comparable和Comparator
在java中要实现自定义类的比较,提供了以下两个接口: Comparable(内部排序) int compareTo(Object obj);返回值为int,默认升序排序 Comparator(外部排 ...
- Java的比较器Comparable与Comparator
在Java中有两个比较器:Comparable.Comparator 对于Integer.Double等等类型,可以直接对他们进行比较,因为已经实现了比较的方式,然而在平时常常会面临需要对集合进行排序 ...
- Java原来如此-比较器(Comparable、Comparator)
有时候需要对Collection或者不为单一数字的Array进行比较,有两种方法,1是实现Comparable接口,2是实现Comparator接口. 1.ComParable接口 Comparabl ...
- 小白养成记——Java比较器Comparable和Comparator
一.使用情景 1. 调用Arrays.sort()方法或Collections.sort()方法对自定义类的对象排序 以Arrays.sort()为例.假定有如下自定义的Person类 1 publ ...
- Comparable和Comparator的区别
Comparable Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较 ...
- comparable和comparator
Comparable Comparable可以认为是一个内部比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,在compareTo方法中指定具体的比较方法. comp ...
- 关于comparable与comparator的用法(即自定义集合框架用法 )
package javastudy; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; ...
- Comparable和Comparator的区别&Collections.sort的两种用法
在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...
随机推荐
- PyCharm无法激活
如果你激活软件遇到问题 (Pycharm.GoLand.idea.phpstorm.webstorm.sublime.ultraEdit.win10等等) 比如: 激活框提示Key is invali ...
- 12.Flask-Restful
定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...
- 强如 Disruptor 也发生内存溢出?
前言 OutOfMemoryError 问题相信很多朋友都遇到过,相对于常见的业务异常(数组越界.空指针等)来说这类问题是很难定位和解决的. 本文以最近碰到的一次线上内存溢出的定位.解决问题的方式展开 ...
- redis 系列20 服务器上
一.客户端与服务端交互 本篇简单介绍下服务器,服务器运行涉及的内部原理知识很多,主要了解Redis服务器内部要做哪些事情,需要开发人员去干预的比较少.Redis服务器负责与多个客户端建立网络连接,处理 ...
- 前端笔记之jQuery(下)事件&节点操作&净位置&拖拽&页面卷动值&遍历JSON
一.监听事件大全 1.1 JavaScript事件 onblur 元素失去焦点 onchange 用户改变域的内容 onclick 鼠标点击某个对象 ondblclick 鼠标双击某个对象 onfoc ...
- 为 docker 中的 nginx 配置 https
没有 https 加持的网站会逐渐地被浏览器标记为不安全的,所以为网站添加 https 已经变得刻不容缓.对于商业网站来说,花钱购买 SSL/TLS 证书并不是什么问题.但对于个人用户来说,如果能有免 ...
- 深度学习框架Keras介绍及实战
Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK, 或者 Theano 作为后端运行.Keras 的开发重点是支持快速的实验.能够以最小的时延 ...
- ROS笔记 Topics
http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics rostopic rqt_graph rosmsg rqt_graph 一个用于查看topi ...
- 大前端的自动化工厂(2)—— SB Family
原文链接:https://bbs.huaweicloud.com/blogs/53c0c3509b7a11e89fc57ca23e93a89f 我坦白我是标题党,SB只是SCSS-Bourbon的简写 ...
- 在ASP.NET MVC 项目中 使用 echarts 画统计图
echarts 官方地址:http://echarts.baidu.com/ 一.根据图中的数据怎么从数据库中获取并组装成对应格式: 从数据库中获取对应数据,然后在项目中引用Newtonsoft.Js ...