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 ...
随机推荐
- C# Post HTTP Request
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Secu ...
- shell利用数组分割组合字符串
#!/bin/bash #接收脚本参数如[sh a.txt .0_3_4_f_u_c_k_8080] a=$ #把参数分割成数组 arr=(${a//_/ }) #显示数组长度 #echo ${#ar ...
- DOM常用事件绑定方式与实例
一.常用的事件 onclick 点击事件 模态框实例 <input type="button" id="b1" style="width:50p ...
- Xshell登录Ubuntu12.04
Ubuntu安装ssh服务: sudo apt-get install openssh-server 打开Xshell,选择“新建”,“连接”设置里选择SSH,主机填入需要连接的主机的IP地址.在“用 ...
- hdu 3016 Man Down
题意:给你n个板子,初始100生命,到达每个板子加血或者扣血,求从最上面的板子落到地面的最优解 题解:对于每一个木板,只有从左下或者从右下,所以从下往上来看,到达第n个木板的最优解为 dp[n] = ...
- DB2 create tablespace
db2手工创建表空间 db2 v10.11.1 创建系统管理使用文件目录的表空间 db2 "create tablespace newtbs01 managed by system usin ...
- html笔记篇-Sublime、Markdown
1.sublime优点:跨平台(Linux,Windows和Mac OS X). 体积小运行速度快. 支持编译功能 .支持大量的插件 2.sublime快速生成html文档: !+tab键 ...
- Prometheus 函数
函数列表 一些函数有默认的参数,例如:year(v=vector(time()) instant-vector).v是参数值,instant-vector是参数类型.vector(time())是默认 ...
- {MySQL数据库初识}一 数据库概述 二 MySQL介绍 三 MySQL的下载安装、简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 初识sql语句
MySQL数据库初识 MySQL数据库 本节目录 一 数据库概述 二 MySQL介绍 三 MySQL的下载安装.简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 ...
- nowcoder 206A - Birthday - [最小费用最大流]
题目链接:https://www.nowcoder.com/acm/contest/206/A 题目描述 恬恬的生日临近了.宇扬给她准备了一个蛋糕.正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为 ...