Java中Collections类的排序sort函数两种用法
java中的Colletions类主要实现列表List的排序功能。根据函数参数的传递,具体的排序可以分为 :
1. 自然排序(natural ordering)。
函数原型:sort(List<T> list)
说明:参数是要参与排序列表的List对象
实例说明:参与排序的列表的元素Student必须实现Comparable接口的
public int compareTo(Object o) 方法,在里面写对比的原则。
然后调用Colletions.sort(排序对象的列表)
请看如下示例:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
class ArrayListTest{
public static void printElements(Collection c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args){
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(2,"aora"));
al.add(new Student(1,"longyu"));
al.add(new Student(3,"goso"));
Collections.sort(al);
printElements(al);
}
}
class Student implements Comparable{
int num;
String name;
Student(int num,String name){
this.num = num;
this.name = name;
}
public int compareTo(Object o) {
Student s = (Student)o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
};
public String toString(){
return "num = " + this.num + ",name = " + this.name;
}
}
2. 实现比较器(Comparator)接口。
函数原型:sort(List<T> list, Comparator<? super T> c)
说明:第一个参数同左,第二个参数是构建对比规则的对比器Comparator。
实例说明(如下):在参与排序的列表的元素Student中写一个内部类作为
Student的对比器,这个对比器要实现Comparator接口的public int compare(Object o1,
Object o2)方法,然后调用Colletions.sort(排序对象的列表,对比器)
请看如下示例:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Comparator;
class ArrayListTest{
public static void printElements(Collection c){
8 Iterator it = c.iterator();
while(it.hasNext()){
10 System.out.println(it.next());
}
}
public static void main(String[] args){
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(2,"qingan"));
al.add(new Student(1,"longyu"));
al.add(new Student(3,"goso"));
al.add(new Student(2,"aora"));
19 Collections.sort(al,new Student.studentComparator());
20 printElements(al);
}
}
class Student{
int num;
String name;
Student(int num,String name){
this.num = num;
this.name = name;
}
static class studentComparator implements Comparator{
public int compare(Object o1,Object o2){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
34 int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
// 注意:此处在对比num相同时,再按照name的首字母比较。
if(result == 0){
37 result = s1.name.compareTo(s2.name);
38 }
return result;
}
}
public String toString(){
return "num = " + this.num + ",name = " + this.name;
}
}
(转http://viver120.blog.163.com/blog/static/60072482013010111228695/)
Java中Collections类的排序sort函数两种用法的更多相关文章
- Comparable和Comparator的区别&Collections.sort的两种用法
在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...
- Java中Collections类详细用法
1.sort(Collection)方法的使用(含义:对集合进行排序). 例:对已知集合c进行排序? public class Practice { public static void main(S ...
- java基础 -- Collections.sort的两种用法
/** * @author * @version * 类说明 */ package com.jabberchina.test; import java.util.ArrayList; import j ...
- java基础——Collections.sort的两种用法
Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式: public static <T extends Comparable<? ...
- java基础—— Collections.sort的两种用法
package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...
- Collections.sort的两种用法 转
/** * @author guwh * @version 创建时间:2011-11-3 上午10:49:36 * 类说明 */ package com.jabberchina.test; impor ...
- Java中x+=y和x=x+y两种实现的区别
先看下边两段代码,各有什么错? 例一: short s1 = 1; s1 = s1 + 1; 例二: short s1 = 1; s1 += 1; 第一段代码无法通过编译,由于 s1+1 在运算时会自 ...
- JAVA中String类的方法(函数)总结--JAVA基础
1.concat()方法,当参数为两字符串时,可实现字符串的连接: package cn.nxl123.www; public class Test { public static void main ...
- Collections.sort的两种用法
http://gwh-08.iteye.com/blog/1233401/ class Foo implements Comparable<Foo>{ @Override public i ...
随机推荐
- 使用log4cplus时遇到的链接错误:无法解析的外部符号 "public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,
#include "stdafx.h" #include <log4cplus/logger.h> #include <log4cplus/loggingmacr ...
- VS2017 配置ImageMagick
以下配置仅供参考,我配置完了怎样都用不了... 直接下载源码使用VS进行编译. 源码下载地址:http://imagemagick.org/script/install-source.php#wind ...
- Ubuntu 下 Sublime 无法输入中文?(已解决)
在 Ubuntu 里安装了 Sublime 却不能输入中文? 这可不好. 怎么办呢? Follow Me! 1 获得 sublime-imfix.c 文件 有 GitHub 账号的,可以从 https ...
- C#自定义Winform无边框窗体
C#自定义Winform无边框窗体 在实际项目中,WinForm窗体或者控件不能满足要求,所以就需要自己设计窗体等,当然设计界面可以用的东西很多,例如WPF.或者一些第三方的库等.本例中将采用WinF ...
- modbus ASCII和MODBUS RTU区别
下表是MODBUS ASCII协议和RTU协议的比较: 协议 开始标记 结束标记 校验 传输效率 程序处理 ASCII :(冒号) CR,LF LRC 低 直观,简单,易调试 RTU 无 无 CRC ...
- 企业应用打包的时候 修改ipa包的bundle identifier
1.将ipa包后缀改为.zip,解压,之后打开包文件,找到info.plist文件后,修改相应的项就可以了.把修改后的文件重新压缩成zip,把zip改为ipa,替代原来的ipa,就可以了. 解决这个问 ...
- js document.activeElement 获得焦点的元素
<body> <input type="text" id="test" value="ajanuw"> <sc ...
- CentOS 7.4下使用yum安装MySQL5.7.20 最简单的 (引用)
引用 https://blog.csdn.net/z13615480737/article/details/78906598 CentOS7默认数据库是mariadb, 但是 好多用的都是mysql ...
- SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句
SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...
- spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案
Springboot中静态资源和拦截器处理(踩了坑) 背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...