JAVA集合四:比较器--类自定义排序
参考链接:
前言
对于JAVA集合,都能够用集合的工具类Collections 提供的方法:
- Collections.sort(List list)
- Collections.sort(List list, Comparator c)
来进行排序。很多时候,集合中存储的不是基本的数据类型,而是我们自己定义的类的对象,我们希望用自己的排序方式对集合中的元素进行排序,我们可以通过让类实现Comparable接口来自定义排序方式,也可以采用创建匿名内部类的做法进行自定义排序。
实现Comparable接口
让待排序的类继承Comparable接口,并实现compareTo方法(相当于给类提供了排序依据)
package blog; /*
* Student 类
*/
public class Student implements Comparable<Student>{
private String name;
private int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
}
//实现对象比较的方法(id从小到大排序)
@Override
public int compareTo(Student o) {
if(id < o.id)
return -1;
else
return 1;
}
}
直接使用Collections.sort(List list)进行排序
package blog; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("xsy", 2));
students.add(new Student("theory", 1));
students.add(new Student("衍射",3)); //排序前
System.out.println("排序前:");
for(Student student : students) {
System.out.println(student);
} //排序后(按照id从小到大)
Collections.sort(students);
System.out.println("排序后:");
for(Student student : students) {
System.out.println(student);
}
}
}
排序前:
Student [name=xsy, id=2]
Student [name=theory, id=1]
Student [name=衍射, id=3]
排序后:
Student [name=theory, id=1]
Student [name=xsy, id=2]
Student [name=衍射, id=3]
JAVA创建匿名内部类
类不需要继承接口,只需要在排序时重写Comparator的compare方法:
package blog;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("xsy", 2));
students.add(new Student("theory", 1));
students.add(new Student("衍射",3));
//排序前
System.out.println("排序前:");
for(Student student : students) {
System.out.println(student);
}
//排序后(按照id从小到大)-- 重写Comparator的compare方法
Collections.sort(students, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Student && o2 instanceof Student) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
return s1.getId()-s2.getId();
}
return 0;
}
});
System.out.println("排序后:");
for(Student student : students) {
System.out.println(student);
}
}
}
运行结果如下:
排序前:
Student [name=xsy, id=2]
Student [name=theory, id=1]
Student [name=衍射, id=3]
排序后:
Student [name=theory, id=1]
Student [name=xsy, id=2]
Student [name=衍射, id=3]
JAVA集合四:比较器--类自定义排序的更多相关文章
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
- [Effective Java]第四章 类和接口
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- JAVA(四)类集/枚举
成鹏致远 | lcw.cnblog.com |2014-02-04 JAVA类集 1.认识类集 类集的作用 类集实际上就是一个动态的对象数组,与一般的对象数组不同,类集中的对象内容可以任意扩充 类集的 ...
- java集合框架——工具类
一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...
- Java 集合的工具类Collections的常用方法
Collections类 java.utils.Collections是集合工具类,用来对集合进行操作. Collections类的常用方法 这里介绍四个常用方法: addAll(Collection ...
- Java集合框架和数组的排序(转载)
Java集合框架(*Collection)*和数组的排序 根据约定,在使用java编程的时候应尽可能的使用现有的类库,当然你也可以自己编写一个排序的方法,或者框架,但是有几个人能写得比JDK里的还 ...
- Java集合----Collection工具类
Collections 工具类 Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了 ...
- JAVA 使用Comparator接口实现自定义排序
1.原则 Comparator接口可以实现自定义排序,实现Comparator接口时,要重写compare方法: int compare(Object o1, Object o2) 返回一个基本类型的 ...
随机推荐
- 解决:Invalid character found in the request target.The valid characters are defined in RFC 7230 and RF
背景 在将tomcat升级到7.0.81版后,发现系统的有些功能不能使用了,查询日志发现是有些地址直接被tomcat认为存在不合法字符,返回HTTP 400错误响应,错入信息如下: 原因分析 经了解, ...
- SpringCloud 入门(三)
前文我们介绍了简单的创建一个客户端,并介绍了它是如何提供服务的,接下来介绍它的另外一个组件:zuul. zuul 提供了微服务的网关功能,通过它提供的接口,可以转发不同的服务,可以当作一个中转站. 搭 ...
- 《Elasticsearch 权威指南》阅读笔记
书籍地址 https://www.elastic.co/guide/cn/elasticsearch/guide/current/languages.html
- vue开发搭建(npm安装 + vue脚手架安装)
一.概念 1.npm: Nodejs下的包管理器. 2.webpack: 它主要的用途是通过CommonJS的语法,把所有浏览器端需要发布的静态资源,做相应的准备,比如资源的合并和打包. 3.vue ...
- 利用oracle数据库闪回功能将oracle数据库按时间点恢复
oracle更新脚本把原数据冲了,并且没有备份,急煞我也 解决办法: oracle数据库有闪回功能: select * from tab 可以查出已被删除的表 ...
- 硬刚 lodash 源码之路,compact & concat
前置 本篇随笔包含 _.compact 和 _.concat 及其依赖的工具函数. 你可能需要一些 JavaScript 基础知识才能看懂一些没有注释的细节. compact _.compact(ar ...
- [译]高性能缓存库Caffeine介绍及实践
概览 本文我们将介绍Caffeine-一个Java高性能缓存库.缓存和Map之间的一个根本区别是缓存会将储存的元素逐出.逐出策略决定了在什么时间应该删除哪些对象,逐出策略直接影响缓存的命中率,这是缓存 ...
- 【板子】数论基础(持续更新ing...)
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...
- P2607[ZJOI2008] 骑士 题解
题目 Z 国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的 Y 国发动了一场针对 Z 国的侵略战争.战火绵延五 ...
- 最近用unity写三消游戏,mark一个准备用的unity插件,用来控制运动。
http://www.pixelplacement.com/itween/index.php itween 听说还不错!