Java自定义排序
实现Comparator接口
实现该接口需要重写compare()方法
Arrays.sort(students, new Comparator<Student>() {
@Override
// 升序 o1 - o2
// 降序 o2 - o1
public int compare(Student o1, Student o2) {
if(o1.age == o2.age)
return o1.id - o2.id;
return o2.age - o1.age;
}
});
实现Comparable接口
实现该接口需要重写compareTo()方法
class Student implements Comparable<Student>{
int id;
int age;
public Student(int id,int age){
this.id = id;
this.age = age;
}
@Override
// 升序 this - o
// 降序 o - this
public int compareTo(Student o) {
if(this.age == o.age)
return o.id - this.id;
return this.age - o.age;
}
}
完整代码
import java.util.Arrays;
import java.util.Comparator;
public class 自定义排序 {
public static void main(String[] args){
Student[] students = new Student[10];
students[0] = new Student(1001,5);
students[1] = new Student(1002,4);
students[2] = new Student(1003,3);
students[3] = new Student(1004,2);
students[4] = new Student(1005,1);
students[5] = new Student(1006,1);
students[6] = new Student(1007,2);
students[7] = new Student(1008,3);
students[8] = new Student(1009,4);
students[9] = new Student(1011,5);
System.out.println("Comparator:根据年龄降序排序,如果年龄相同,则根据id升序");
// 根据年龄降序排序,如果年龄相同,则根据id升序
Arrays.sort(students, new Comparator<Student>() {
@Override
// 升序 o1 - o2
// 降序 o2 - o1
public int compare(Student o1, Student o2) {
if(o1.age == o2.age)
return o1.id - o2.id;
return o2.age - o1.age;
}
});
for(Student s : students)
System.out.println(s.id + " " + s.age);
System.out.println("Comparable:根据年龄升序排序,如果年龄相同,则根据id降序");
// 根据年龄升序排序,如果年龄相同,则根据id降序
Arrays.sort(students);
for(Student s : students)
System.out.println(s.id + " " + s.age);
}
}
class Student implements Comparable<Student>{
int id;
int age;
public Student(int id,int age){
this.id = id;
this.age = age;
}
@Override
// 升序 this - o
// 降序 o - this
public int compareTo(Student o) {
if(this.age == o.age)
return o.id - this.id;
return this.age - o.age;
}
}
运行结果
Comparator:根据年龄降序排序,如果年龄相同,则根据id升序
1001 5
1011 5
1002 4
1009 4
1003 3
1008 3
1004 2
1007 2
1005 1
1006 1
Comparable:根据年龄升序排序,如果年龄相同,则根据id降序
1006 1
1005 1
1007 2
1004 2
1008 3
1003 3
1009 4
1002 4
1011 5
1001 5
Java自定义排序的更多相关文章
- LeetCode1029 两地调度(贪心+java自定义排序回顾)
题目: 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵 ...
- Java自定义排序:继承Comparable接口,重写compareTo方法(排序规则)
代码: 1 import java.util.*; 2 3 /** 4 * 学习自定义排序:继承Comparable接口,重写compareTo方法(排序规则). 5 * TreeMap容器的Key是 ...
- Java集合框架实现自定义排序
Java集合框架针对不同的数据结构提供了多种排序的方法,虽然很多时候我们可以自己实现排序,比如数组等,但是灵活的使用JDK提供的排序方法,可以提高开发效率,而且通常JDK的实现要比自己造的轮子性能更优 ...
- 【Java】Treeset实现自定义排序
两个类,一个学生类,含姓名和出生日期两个属性:还有一个学生排序类,重写compare函数,自定义排序规则是先比较出生日期,如果相同再比较姓名字母 package birthday; import ja ...
- java编程排序之自定义类型的集合,按业务需求排序
自定义引用类型放入集合中,按实际业务需求进行排序的两种思路 第一种思路: (1)自定义实体类实现java.lang.Comparable接口,重写public int compareTo(Object ...
- java编程排序之内置引用类型的排序规则实现,和自定义规则实现+冒泡排序运用
第一种排序:[冒泡排序]基本数据类型的排序. [1]最简易的冒泡排序.效率低.因为比较的次数和趟数最多. /** * 最原始的冒泡排序. * 效率低. * 因为趟数和次数最多.都是按最大化的循环次数进 ...
- JAVA 使用Comparator接口实现自定义排序
1.原则 Comparator接口可以实现自定义排序,实现Comparator接口时,要重写compare方法: int compare(Object o1, Object o2) 返回一个基本类型的 ...
- java自定义类型 比较排序 Comparator接口
String service_time = "6:00:00,7:00:00,8:00:00,9:00:00,10:00:00,11:00:00,12:00:00,13:00:00,14:0 ...
- Java 中HashTable、HashMap、TreeMap三者区别,以及自定义对象是否相同比较,自定义排序等
/* Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. Map |--Hashtable:底层是哈希表数据结构,不可以存入null键null值.该集合是线程同步的.效率低.基本已废 ...
- LeetCode242 有效的字母异位词(Java字符数组排序&自定义排序记录)
题目: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram& ...
随机推荐
- Arrays.asList()你真的知道怎么用吗?
发现问题 前几天在看别人的项目的时候,发现一个问题,简单复现一下这个问题 // 注意这是一个Integer对象的数组哦 Integer[] arr = new Integer[]{9999,88,77 ...
- [apue] 标准 I/O 库那些事儿
前言 标准 IO 库自 1975 年诞生以来,至今接近 50 年了,令人惊讶的是,这期间只对它做了非常小的修改.除了耳熟能详的 printf/scanf,回过头来对它做个全方位的审视,看看到底优秀在哪 ...
- Codeforces Round #821(Div.2) (A-C) 题解
Codeforces Round #821(Div.2) (A-C) A.Consecutive Sum 大致题意 给定一组共 n 个数据 ,如果俩个数的下标在 mod k 意义下同余,则可以交换a[ ...
- Java开发学习(三十五)----SpringBoot快速入门及起步依赖解析
一.SpringBoot简介 SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程. 使用了 Spring 框架后已经简化了我 ...
- Django 之视图层
JsonResponse 1 json格式的数据有什么用 前后端数据交互需要使用json作为过渡,实现跨语言传输数据 2 前后端方法对应 JSON.stringify() - json.dumps( ...
- k8s中计算资源策略 Limit Range
文章转载自:https://www.kuboard.cn/learning/k8s-advanced/policy/lr.html 默认情况下,容器在 Kubernetes 集群上运行时,不受 计算资 ...
- kvm安装windows使用virtio驱动下载地址
https://dl.fedoraproject.org/pub/alt/virtio-win/latest/images/bin/deprecated-README 老版本下载地址:https:// ...
- 银河麒麟安装node,mysql,forever环境
这就是国产银河系统的界面,测试版本是麒麟V10 链接: https://pan.baidu.com/s/1_-ICBkgSZPKvmcdy1nVxVg 提取码: xhep 一.传输文件 cd /hom ...
- Java程序设计(二)作业
题目1:输入一个三位十进制数,对其每一位进行筛选,逆序组合后输出. package test; import java.util.*; public class test2{ public stati ...
- 设计模式之观察者模式_C++
1 // ADBHelper.cpp : This file contains the 'main' function. Program execution begins and ends there ...