java中的Colletions类主要实现列表List的排序功能。根据函数参数的传递,具体的排序可以分为 :

1.  自然排序(natural ordering)。

函数原型:sort(List<T> list)
说明:参数是要参与排序列表的List对象                                                               
实例说明:参与排序的列表的元素Student必须实现Comparable接口的
public int compareTo(Object o) 方法,在里面写对比的原则。
然后调用Colletions.sort(排序对象的列表)    
请看如下示例:

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. class ArrayListTest{
  8. public static void printElements(Collection c){
  9.   Iterator it = c.iterator();
  10.   while(it.hasNext()){
  11.   System.out.println(it.next());
  12. }
  13. }
  14. public static void main(String[] args){
  15.   ArrayList<Student> al = new ArrayList<Student>();
  16.   al.add(new Student(2,"aora"));
  17.   al.add(new Student(1,"longyu"));
  18.   al.add(new Student(3,"goso"));
  19.   Collections.sort(al);
  20.   printElements(al);
  21. }
  22. }
  23. class Student implements Comparable{
  24.   int num;
  25.   String name;
  26.   Student(int num,String name){
  27.   this.num = num;
  28.   this.name = name;
  29. }
  30.   public int compareTo(Object o) {
  31.     Student s = (Student)o;
  32.     return num > s.num ? 1 : (num == s.num ? 0 : -1);
  33.   };
  34.   public String toString(){
  35.     return "num = " + this.num + ",name = " + this.name;
  36.   }
  37. }

2.  实现比较器(Comparator)接口。

函数原型:sort(List<T> list, Comparator<? super T> c)
说明:第一个参数同左,第二个参数是构建对比规则的对比器Comparator。
实例说明(如下):在参与排序的列表的元素Student中写一个内部类作为
Student的对比器,这个对比器要实现Comparator接口的public int compare(Object o1,
Object o2)方法,然后调用Colletions.sort(排序对象的列表,对比器)

请看如下示例:

  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.Collections;
  4. import java.util.Iterator;
  5. import java.util.Comparator;
  6. class ArrayListTest{
  7.   public static void printElements(Collection c){
  8. 8     Iterator it = c.iterator();
  9.     while(it.hasNext()){
  10. 10       System.out.println(it.next());
  11.     }
  12.   }
  13.   public static void main(String[] args){
  14.     ArrayList<Student> al = new ArrayList<Student>();
  15.     al.add(new Student(2,"qingan"));
  16.     al.add(new Student(1,"longyu"));
  17.     al.add(new Student(3,"goso"));
  18.     al.add(new Student(2,"aora"));
  19. 19     Collections.sort(al,new Student.studentComparator());
  20. 20     printElements(al);
  21.    }
  22. }
  23. class Student{
  24.   int num;
  25.   String name;
  26.   Student(int num,String name){
  27.     this.num = num;
  28.     this.name = name;
  29.   }
  30.   static class studentComparator implements Comparator{
  31.     public int compare(Object o1,Object o2){
  32.       Student s1 = (Student)o1;
  33.       Student s2 = (Student)o2;
  34. 34       int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
  35. // 注意:此处在对比num相同时,再按照name的首字母比较。
  36.       if(result == 0){
  37. 37         result = s1.name.compareTo(s2.name);
  38. 38       }
  39.       return result;
  40.     }
  41. }
  42.   public String toString(){
  43.     return "num = " + this.num + ",name = " + this.name;
  44.   }
  45. }

(转http://viver120.blog.163.com/blog/static/60072482013010111228695/)

Java中Collections类的排序sort函数两种用法的更多相关文章

  1. Comparable和Comparator的区别&Collections.sort的两种用法

    在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...

  2. Java中Collections类详细用法

    1.sort(Collection)方法的使用(含义:对集合进行排序). 例:对已知集合c进行排序? public class Practice { public static void main(S ...

  3. java基础 -- Collections.sort的两种用法

    /** * @author * @version * 类说明 */ package com.jabberchina.test; import java.util.ArrayList; import j ...

  4. java基础——Collections.sort的两种用法

    Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式: public static <T extends Comparable<? ...

  5. java基础—— Collections.sort的两种用法

    package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...

  6. Collections.sort的两种用法 转

    /** * @author guwh * @version 创建时间:2011-11-3 上午10:49:36 * 类说明 */ package com.jabberchina.test; impor ...

  7. Java中x+=y和x=x+y两种实现的区别

    先看下边两段代码,各有什么错? 例一: short s1 = 1; s1 = s1 + 1; 例二: short s1 = 1; s1 += 1; 第一段代码无法通过编译,由于 s1+1 在运算时会自 ...

  8. JAVA中String类的方法(函数)总结--JAVA基础

    1.concat()方法,当参数为两字符串时,可实现字符串的连接: package cn.nxl123.www; public class Test { public static void main ...

  9. Collections.sort的两种用法

    http://gwh-08.iteye.com/blog/1233401/ class Foo implements Comparable<Foo>{ @Override public i ...

随机推荐

  1. day_5.10py 爬妹子图片 mm131

    #目前学的爬虫还有潭州教育的直播课,都是千篇一律的requests urllib 下面这个也是,还没有我后面的下载网易云歌单爽来都用到多线程了不过可以用协程,完全异步 1 #!/usr/bin/env ...

  2. day_5.04py

    Pycharm默认不依赖系统环境下安装的requests 打开 File->Settings->Project:你的Project名 点击“+”号 搜索requests并安装 长见识了 # ...

  3. 180623、Git新建远程分支和删除

    Git新建远程分支和删除 现在我在master分支上,工作目标是干净的,也没有需要commit的: $ git branch * master release $ git status On bran ...

  4. li下的ul----多级列表

    <ul id="ul_Style1"> <li>第1级第1行</li> <li> <ul id="ul_Style2 ...

  5. Excel相关操作

    public static bool DataSetToExcel(DataSet dataSet, string filePath, bool isShowExcle = true) { DataT ...

  6. process 多进程写法 multiprocessing

    from multiprocessing import Process def   f1(n):#普通 print(f1) if __name__ == '__main__': lst = [] fo ...

  7. CodeForces 1099E - Nice table - [好题]

    题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...

  8. tensorflow 一维卷积 tf.layers.conv1()使用

    在自然语言处理中,主要使用一维的卷积. API tf.layers.conv1d( inputs, filters, kernel_size, strides=1, padding='valid', ...

  9. maven工程之pom模板(hadoop、hive、hbase)

    以下配置文件涵盖了hadoop.hive.hbase开发支持库的配置. 仅需针对maven工程pom.xml文件做相应更改就可以自动生成hadoop开发支持库. <properties>  ...

  10. C和C指针小记(四)-浮点类型

    1.浮点型 浮点数家族包括:float,double,long double. ASCII标准规定:long double 至少和 double 一样长,而 double 至少和float 一样长.同 ...