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函数两种用法的更多相关文章

  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. NUC972----最简单的驱动(转)

    1.新建文本文档,重命名为 hello_dev.c (驱动的开发同应用的开发一样,也是在文本文档下开发的). 2.包含头文件 内核模块需要包含内核相关头文件,不同模块根据功能的差异,所需要的头文件也不 ...

  2. 【CF576E】Painting Edges 线段树按时间分治+并查集

    [CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...

  3. rxjs 常用的管道操作符

    操作符文档 api 列表 do -> tap catch -> catchError switch -> switchAll finally -> finalize map s ...

  4. 自定义元素 v1:可重用网络组件

    google文档 https://developers.google.cn/web/fundamentals/web-components/customelements 兼容性 https://can ...

  5. python中的细节

    # 1 # li = ['a', 'b', 'c', 'd','e'] # print(li[10:]) #[] 不报错 # 2 # 这不是True或False,而是值 # 另外,优先级 not &g ...

  6. F#周报2018年第48期

    新闻 F#2018年圣诞日历 Mac上的Visual Studio 2017新版本7.7 Rider 2018.3将引入远程调试功能 Visual Studio 2017新版本15.9.3 视频及幻灯 ...

  7. day0320 时间模块 collection模块

    一. TIME模块 python提供了一个time和calendar模块可以用于格式化日期和时间. 时间间隔一秒为单位. 每个时间戳都以1970年1月1日午夜经过多长时间来表示. 1.时间戳 函数ti ...

  8. [linux] 对一个虚拟机的研究

    今天拿到了一个vmware的虚拟机硬盘镜像,是其他公司的演示产品. 启动之后是带着ubuntu字样的grub.进入系统之后也不是shell,而是一个定制的命令行.所以如果想了解细节的话,只能单独挂硬盘 ...

  9. SoftPixelEngin

    目的,拓展知识. 1.CMake夸平台构建; 2.RederSystem; 3.Shaderlibrary: http://blog.csdn.net/ym19860303/article/detai ...

  10. ehlib预览打印的使用

    ehlib支持预览打印功能,可以省去重新制作报表的麻烦,经过一天的努力,基本上解决了这个问题.把解决方法写出来,同行的朋友可以参考,同时为自己做个学习笔记.     首先,需要放置PrintDBGri ...