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.sortArrays.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用法的更多相关文章

  1. 比较器comparable与comparator的使用

    在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...

  2. 比较器Comparable和Comparator

    在java中要实现自定义类的比较,提供了以下两个接口: Comparable(内部排序) int compareTo(Object obj);返回值为int,默认升序排序 Comparator(外部排 ...

  3. Java的比较器Comparable与Comparator

    在Java中有两个比较器:Comparable.Comparator 对于Integer.Double等等类型,可以直接对他们进行比较,因为已经实现了比较的方式,然而在平时常常会面临需要对集合进行排序 ...

  4. Java原来如此-比较器(Comparable、Comparator)

    有时候需要对Collection或者不为单一数字的Array进行比较,有两种方法,1是实现Comparable接口,2是实现Comparator接口. 1.ComParable接口 Comparabl ...

  5. 小白养成记——Java比较器Comparable和Comparator

    一.使用情景 1.  调用Arrays.sort()方法或Collections.sort()方法对自定义类的对象排序 以Arrays.sort()为例.假定有如下自定义的Person类 1 publ ...

  6. Comparable和Comparator的区别

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

  7. comparable和comparator

    Comparable Comparable可以认为是一个内部比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,在compareTo方法中指定具体的比较方法. comp ...

  8. 关于comparable与comparator的用法(即自定义集合框架用法 )

    package javastudy; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; ...

  9. Comparable和Comparator的区别&Collections.sort的两种用法

    在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...

随机推荐

  1. [Swift]LeetCode895. 最大频率栈 | Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  2. kubernetes---kubectl 管理集群资源

    由于我现在的集群是把虚拟机的master文件直接拷贝过来的,所以之前的node节点是不存在的,只有k8s-ubuntu-1是新加入的,所以我要把上面之前创建的资源删除 删除deployment--&g ...

  3. Java面试题中的Redis大合集,所有你想找的都在这里!

    概述 Redis 是一个开源的,基于内存的结构化数据存储媒介,可以作为数据库.缓存服务或消息服务使用.``` Redis 支持多种数据结构,包括字符串.哈希表.链表.集合.有序集合.位图.Hyperl ...

  4. 谷歌浏览器的各种插件网址Chrome插件(谷歌浏览器)-超级详细

    各种各样的插件,可以查找对自己有用的,自行下载安装 http://chromecj.com/

  5. CentOS 7 源码编译安装 Nginx

    这里安装的是nginx 1.14版本 1.下载源码 #下载 wget http://nginx.org/download/nginx-1.14.0.tar.gz #解压 tar -xzf nginx- ...

  6. 实现网站页面的QQ临时会话,分享到空间微博等按钮.

    一 qq临时会话 要实现qq临时会话首先要到qq在线状态官网开通qq在线状态,其中临时对话也分为加密和未加密. 1.1:加密模式 <a target="_blank" hre ...

  7. 『战略游戏 最大利润 树形DP』

    通过两道简单的例题,我们来重新认识树形DP. 战略游戏(luoguP1026) Description Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要 ...

  8. asp.net core 系列 1 概述

    一.   概述 ASP.NET Core 是一个跨平台的高性能开源框架,可以用来:建置 Web 应用程序和服务.IoT应用和移动后端.在 Windows macOS 和 Linux 上使用喜爱的开发工 ...

  9. [Leetcode]674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  10. 2018-7-27银行卡bin大全-根据银行卡开头查银行

    支付宝卡号验证工具 https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=银 ...