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 ...
随机推荐
- Vuex 的使用 State Mutation Getter Action
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); /*1.state在vuex中用于存储数据*/ var state={ cou ...
- Python3基础 函数 多值参数 元组与字典形式(键值对分别指出)
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- 报错:flutter: Another exception was thrown: Could not find a generator for route RouteSettings
原因是一个工程中多次使用MaterialApphttps://stackoverflow.com/questions/49132299/could-not-find-a-generator-for-r ...
- 头文件里声明和定义,Qt编译不过问题
1.现象1 Qt5.2.1,新建头文件,声明一个类,然后在此头文件中实现类的static变量和方法,但是编译不过,显示:multiple definition of `xxx'. 2.现象2 在高版本 ...
- Eclipse注释模版
打开Window->Preferences->Java->Code Style->Code Templates 点击"Import",导入模板codetem ...
- Linux配置Docker镜像加速器
Docker默认镜像为官方镜像,可以配置成国内加速器提高速度 登录阿里云控制台,搜索容器镜像服务获取到镜像加速服务地址 新建配置文件 /etc/docker/daemon.json 输入以下内容 { ...
- Dotmemory 内存分析工具的操作手册
教程一.开始学习dotmemory 在本教程中,我们将学习如何运行dotMemory内存快照.此外,我们将简要地看看dotMemory的用户界面和基本分析的概念.考虑dotMemory本教程作为起点 ...
- Netty学习笔记(四)——实现dubbo的rpc
1.rpc基本介绍 RPC ( Remote Procedure Call) -远程过程调用,是一个计算机通信协议.该协议允许运行于一台计算机的程序调用另一台计算机的子程序,两个或多个应用程序分布不同 ...
- Linux【Ubuntu】安装docker
内核要大于3.10才能安装docker 查看内核 uname -r 安装yum命令 sudo apt install yum 由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改,故添加使用 ...
- rtsp向rtmp推流
package com.awifi.video.media.test; import org.bytedeco.javacpp.avcodec; import org.bytedeco.javacv. ...