list集合排序
https:/blog.csdn.net/veryisjava/article/details/51675036
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(5);
nums.add(1);
nums.add(0);
System.out.println(nums);
Collections.sort(nums);
System.out.println(nums);
}
test1
输出结果:
[3, 5, 1, 0]
[0, 1, 3, 5]
package core.java.collection.collections;
public class User implements Comparable<User>{
private int score;
private int age;
public User(int score, int age){
super();
this.score = score;
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(User o) {
int i = this.getAge() - o.getAge();//先按照年龄排序
if(i == 0){
return this.score - o.getScore();//如果年龄相等了再用分数进行排序
}
return i;
}
}
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
users.add(new User(78, 26));
users.add(new User(67, 23));
users.add(new User(34, 56));
users.add(new User(55, 23));
Collections.sort(users);
for(User user : users){
System.out.println(user.getScore() + "," + user.getAge());
}
}
test2
输出结果:
55,23
67,23
78,26
34,56
=======================
Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)
package core.java.collection.collections;
public class Students {
private int age;
private int score;
public Students(int age, int score){
super();
this.age = age;
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
public static void main(String[] args) {
List<Students> students = new ArrayList<Students>();
students.add(new Students(23, 100));
students.add(new Students(27, 98));
students.add(new Students(29, 99));
students.add(new Students(29, 98));
students.add(new Students(22, 89));
Collections.sort(students, new Comparator<Students>() {
@Override
public int compare(Students o1, Students o2) {
int i = o1.getScore() - o2.getScore();
if(i == 0){
return o1.getAge() - o2.getAge();
}
return i;
}
});
for(Students stu : students){
System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
}
}
test3
输出结果:
score:89:age22
score:98:age27
score:98:age29
score:99:age29
score:100:age23
从上面的例子我们可以看出Students类没有实现Comparable<T>接口,只是在sort()方法
中多传入一个参数,只不过该参数是一个接口我们需要实现其compare方法。
list集合排序的更多相关文章
- Java比较器对数组,集合排序一
数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...
- ArrayList集合排序
using System;using System.Collections;using System.Collections.Generic;using System.Text; namespace ...
- 【Java进阶】---map集合排序
map集合排序 这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题. 比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按 ...
- CopyOnWriteArrayList集合排序异常问题
1.集合自定义排序实现 对List集合的自定义排序想必大家都知道要使用如下的方式,通过实现Comparator接口并实现compare方法来实现. /** * * @方法名 changeChain * ...
- 二维码扫描&集合排序
一.二维码扫描机制 二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利用构 ...
- .Net中集合排序的一种高级玩法
背景: 学生有名称.学号, 班级有班级名称.班级序号 学校有学校名称.学校编号(序号) 需求 现在需要对学生进行排序 第一排序逻辑 按学校编号(序号)排列 再按班级序号排列 再按学生学号排列 当然,在 ...
- Java集合排序及java集合类详解--(Collection, List, Set, Map)
1 集合框架 1.1 集合框架概述 1.1.1 容器简介 到目前为止,我们已经学习了如何创建多个不同的对象,定义了这些对象以后,我们就可以利用它们来做一 ...
- Java提高(5)---map集合排序
map集合排序 这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题. 比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按照成绩的好坏进行排序 ...
- 集合排序 Comparator和Comparable的使用区别
Java 排序 Compare Comparator接口 Comparable接口 区别 在Java中使用集合来存储数据时非常常见的,集合排序功能也是常用功能之一.下面看一下如何进行集合排序,常用的 ...
- map集合排序
默认情况下,HashMap.HashTable.TreeMap.LinkedHashMap的排列顺序比较: package com.per.sdg.demo; import java.util.Has ...
随机推荐
- [db]mysql全量迁移db
机房要裁撤, 原有的老业务机的mysql需要迁移到新的. 方案1: 全量打包拷贝data目录, 发现拷过去各种毛病 方案2: mysqldump逻辑导出解决问题 新的db刚安装好. 步骤记录下. # ...
- moving-files-from-one-linux-server-to-another-using-scp
https://www.tecmint.com/scp-commands-examples/ https://haydenjames.io/linux-securely-copy-files-usin ...
- ionic入门教程-ionic路由详解(state、route、resolve)(转)
http://blog.csdn.net/onil_chen/article/details/51758696?appinstall=0 今天好好的跟大家讲讲ionic的路由配置. 问到的朋友有点多, ...
- 【javascript】定时器组件
'use strict'; module.exports = function() { this.timer = {}; this.config = {}; // 初始化参数 this.init = ...
- k8s的使用
. 查看 kubectl 的状态 kubectl version . 查看集群信息 kubectl cluster-info . 查看节点信息 kubectl get nodes . 创建一个发布 k ...
- IIS 请求 超时设置
asp.net 默认的 session state 模式是 in proc(进程内),数据是在网站的应用程序池里面保存的.这样在 web.config 设置的超时时间,是在应用程序池没有发生回收的基础 ...
- 【App】Android Studio 海马玩
一.工程创建及配置 1.gradle环境变量 2.首次创建工程慢:https://www.cnblogs.com/xiadewang/p/7820377.html 二.海马玩虚拟机 C:\Users\ ...
- 局域网ARP攻击防护
通过借助一些安全软件来实现局域网ARP检测及防御功能. A.电脑管家 电脑管家--工具箱--下载ARP防火墙模块 不支持window2003 B.服务器安全狗 Windows版下载:http://fr ...
- GCH实践经验
服务器: 终端整机: 办公套件: 杀毒软件: 打印机:
- [原]Failed connect to mirrors.cloud.aliyuncs.com:80; Connection refused
web site : https://opsx.alibaba.com/mirror 运行后出现下面的Error: base//x86_64/other_db FAILED http://mirror ...