java中对List中的元素进行排序
Collections对List集合中的数据进行排序
有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到
Java中提供的对集合进行操作的工具类Collections,其中的sort方法
No1.先看一个简单的例子:
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(9);
nums.add(7);
nums.add(1);
nums.add(0);
System.out.println(nums);
Collections.sort(nums);
System.out.println(nums);
}
结果如下:
[2, 9, 7, 1, 0]
[0, 1, 2, 7, 9]
建立一个No2代码要用的entity,如下:
import java.util.Date;
public class Record implements Comparable<Record> {
private String name;
private int age;
private Date start;
public Record(String name, int age, Date start) {
this.name = name;
this.age = age;
this.start = start;
}
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 Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
@Override
public int compareTo(Record o) {
int nameIndex = this.name.compareTo(o.name);
int ageIndex = 0;
int startIndex = 0;
// 姓名一样则比较年龄
if (nameIndex == 0) {
ageIndex = this.age - o.age;
}
// 年龄一样则比较开始时间
if (ageIndex == 0) {
boolean isAfter = this.start.after(o.start);
if (isAfter) {
startIndex = 1;
} else {
startIndex = -1;
}
}
return nameIndex+ageIndex+startIndex;
}
}
No2.稍复杂一些的排序:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class ListOrder {
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Record> records = new ArrayList<Record>();
records.add(new Record("a", 2, df.parse("2015-12-16")));
records.add(new Record("d", 8, df.parse("2015-12-16")));
records.add(new Record("a", 1, df.parse("2019-12-16")));
records.add(new Record("x", 0, df.parse("2014-12-16")));
records.add(new Record("a", 1, df.parse("2018-12-16")));
for (Record record : records) {
System.out.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
Collections.sort(records);
System.out.println("--------------------------------------");
for (Record record : records) {
System.out.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
}
}
输出结果:
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
--------------------------------------
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)
所使用的entity:
import java.util.Date;
public class Record {
private String name;
private int age;
private Date start;
public Record(String name, int age, Date start) {
this.name = name;
this.age = age;
this.start = start;
}
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 Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
}
No3:代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class ListOrder { public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Record> records = new ArrayList<Record>();
records.add(new Record("a", 2, df.parse("2015-12-16")));
records.add(new Record("d", 8, df.parse("2015-12-16")));
records.add(new Record("a", 1, df.parse("2019-12-16")));
records.add(new Record("x", 0, df.parse("2014-12-16")));
records.add(new Record("a", 1, df.parse("2018-12-16")));
for (Record record : records) {
System.out
.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
Collections.sort(records, new Comparator<Record>() {
@Override
public int compare(Record r1, Record r2) {
int nameIndex = r1.getName().compareTo(r2.getName());
int ageIndex = 0;
int startIndex = 0;
// 姓名一样则比较年龄
if (nameIndex == 0) {
ageIndex = r1.getAge() - r2.getAge();
}
// 年龄一样则比较开始时间
if (ageIndex == 0) {
boolean isAfter = r1.getStart().after(r2.getStart());
if (isAfter) {
startIndex = 1;
} else {
startIndex = -1;
}
}
return nameIndex + ageIndex + startIndex;
}
});
System.out.println("--------------------------------------");
for (Record record : records) {
System.out
.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
}
}
結果:
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
--------------------------------------
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
reference:
https://blog.csdn.net/veryisjava/article/details/51675036
https://www.jb51.net/article/72284.htm
https://blog.csdn.net/u012901117/article/details/76853113
java中对List中的元素进行排序的更多相关文章
- java中TreeSet集合如何实现元素的判重
/* 看一下部分的TreeSet源码.... public class TreeSet<E> extends AbstractSet<E> implements Navigab ...
- java集合 collection-list-ArrayList 将自定义对象作为元素存到ArrayList集合中,并去除重复元素。
import java.util.*; /* 将自定义对象作为元素存到ArrayList集合中,并去除重复元素. 比如:存人对象.同姓名同年龄,视为同一个人.为重复元素. 思路: 1,对人描述,将数据 ...
- Java 去除 ArrayList 集合中的重复元素
// One practice package Collection; import java.util.ArrayList; import java.util.Iterator; // 去除 Arr ...
- Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素
ylbtech-Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素 1.返回顶部 1. Java 实例 - 在数组中查找指定元素 Java 实例 以下实例 ...
- Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素-un
ylbtech-Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素 1.返回顶部 1. Java 实例 - 查找数组中的重复元素 Java 实例 以下实例 ...
- Java如何删除数组中的元素?
Java中,如何删除数组元素? 示例 以下示例显示如何从数组中删除元素. package com.yiibai; import java.util.ArrayList; public class Re ...
- Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解
题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...
- java android 将 List中元素互换位置
很多时候我要对List中的元素调换位置,这时候可以用如下代码,意思是将data中的index1与index2元素互换位置 //data 为List Collections.swap(data,inde ...
- leetcode.矩阵.378有序矩阵中第K小的元素-Java
1. 具体题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, ...
- Java实现 LeetCode 703 数据流中的第K大元素(先序队列)
703. 数据流中的第K大元素 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组n ...
随机推荐
- [原][C++]拒绝智能指针与指针混用,常见智能指针问题
公司一个非专科的程序在开发过程中有些毛躁,但是又想使用些新学的技术 这天他正调试呢,发现有一个BUG怎么也找不到原因. 用的好好的内存怎么就突然被删除了呢,好好的指针,怎么就访问越界了呢 没办法,他只 ...
- 【转载】 KL距离(相对熵)
原文地址: https://www.cnblogs.com/nlpowen/p/3620470.html ----------------------------------------------- ...
- windows nginx重启脚本.bat
在nginx.exe目录下新建txt文件写入 @echo offtaskkill /f /fi "IMAGENAME eq nginx.exe"start nginx.exe#pa ...
- python-learning-第二季-画图matplotlib
https://www.bjsxt.com/down/8468.html 绘制方法: 绘制直线: #coding:utf- import matplotlib.pyplot as plt #准备绘制的 ...
- WebSocket始终保持连接的办法
在项目中,后台为了其实把处理结果主动推送个前端,因此使用了WebSocket. 但是问题来了,页面每跳转一次,socket都要重新关闭建立连接.这个资源消耗是很大的,而且线上环境随着并发量的增加会报错 ...
- Dart 中常用的数组操作方法总结
这里总结了一些在 Dart 中常用的数组操作方法,以便查阅. 首先,我们准备两组数据,以方便后面使用: List<Map> students = [ { 'name': 'tom', 'a ...
- bat批处理文件怎么将路径添加到path环境变量中
bat批处理文件怎么将路径添加到path环境变量中 摘自:https://zhidao.baidu.com/question/1887763143433391788.html 永久性的: @echo ...
- Qt编写数据可视化大屏界面电子看板13-基础版
一.前言 之前发布的Qt编写的可视化大屏电子看板系统,很多开发者比较感兴趣,也收到了很多反馈意见,纵观市面上的大屏系统,基本上都是B/S结构的web版本,需要在后台进行自定义配置模块,绑定数据源等,其 ...
- RabbitMQ 入门教程(PHP版) 第六部分:远程调用(RPC)
在云计算环境中,很多时候需要用它其他机器的计算资源,把一部分计算任务分配到其他节点来完成.RabbitMQ 如何使用 RPC 呢?下面将会通过其它节点完成斐波纳契示例. 流程图  当客户端启动时,它 ...
- Python手册 3.7
Python手册 3.7 下载地址:https://pan.baidu.com/s/1dPzwwP3ehnyLUNWTsB2QJg 关注微信公众号获取提取码: 输入:py99 获取提取码