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. 排序算法--冒泡排序(Bubble Sort)_C#程序实现

    排序算法--冒泡排序(Bubble Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来非常困 ...

  2. Maven使用deploy上传jar包到远程库

    一.环境准备 首先需要在本地环境安装好maven,并且在环境变量配置好 二.配置远程库认证 需要在./conf/setting.xml(maven的配置文件,不要弄错)中配置需要远程上传库的地址,用户 ...

  3. AWS EC2 使用root账户密码登陆

    创建亚马逊的云主机EC2会提示下载一个pem的文件,需要使用puttygen转换成ppk私钥,转换过程如下图: 然后在使用putty登录,用户名是ec2-user.下面将修改使用root账户登录: 1 ...

  4. fclose函数无响应

    现象:win32程序在退出时无响应,当一步步跟踪代码时走到fclose. 原因:打开文件在一个线程中,写文件时在另一个线程,在open和write文件时均正常,只有在fclose时出现无响应. 解决: ...

  5. C# MVC+EF—WebApi

    上一篇搭建了页面,这里写下功能. 这里我用WebApi实现数据的增删改查. 一.新建Controller 为了区分明确,我在Controller文件夹下建立一个WebApi文件夹存放. 选中文件夹右键 ...

  6. 170823、SQL Update多表联合更新的方法

    SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...

  7. 爬虫----爬虫请求库requests

    一 介绍 介绍:---------------------------------------------使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的a ...

  8. AIX文件系统和存储部署(转)

    文件系统和存储部署 文件系统的管理是AIX存储结构中的最后一环.定义完lv后,可采用如下两种方式使用lv: a.作为裸设备(raw)使用,一般是数据库型的应用 b.在lv上定义文件系统,并提供文件和数 ...

  9. Hyper-V 与 VMware 和 vbox 的不兼容

    新装的win10 开始先装到docker 装之前必须要装Hyper-V 后来装vbox 并且安装了Centos7系统也用得起,后来不知道怎么win10好像升级了.再启动vbox 开启centos7就报 ...

  10. informix数据库触发器的写法

    虽然有各种数据库,但触发器的原理都是一样的,懂一种数据库的写法就可以了解其他的. 以前写过mysql数据库的触发器,这次写informix的,还顺带看了oracle的,除了语法上的不同,informi ...