Java中类的排序
2016-04-18 16:33
465 查看
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){
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,"qingan"));
al.add(new Student(1,"longyu"));
al.add(new Student(3,"goso"));
al.add(new Student(2,"aora"));
Collections.sort(al,new Student.studentComparator());
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;
int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
// 注意:此处在对比num相同时,再按照name的首字母比较。
if(result == 0){
result = s1.name.compareTo(s2.name);
}
return result;
}
}
public String toString(){
return "num = " + this.num + ",name = " + this.name;
}
}
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){
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,"qingan"));
al.add(new Student(1,"longyu"));
al.add(new Student(3,"goso"));
al.add(new Student(2,"aora"));
Collections.sort(al,new Student.studentComparator());
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;
int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
// 注意:此处在对比num相同时,再按照name的首字母比较。
if(result == 0){
result = s1.name.compareTo(s2.name);
}
return result;
}
}
public String toString(){
return "num = " + this.num + ",name = " + this.name;
}
}
相关文章推荐
- [转]Java中导入、导出Excel
- 堆栈静态理解
- Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchFieldException: versionID
- Spring Task的坑
- JAVA 笔记 (总)
- java版精简搜狗皮肤
- J2SE(七)Java之类的实例化
- javascrip基本知识总结
- Java设计模式系列之状态模式
- Java—JDK的下载与安装
- 三种Java创建线程的方式(Callable,FutureTask)
- java 根据二叉树前序 ,中序求后续
- java 里面的string 和byte[] 怎么互转?
- JDK1.7新特性(二)Fork/Join框架demo
- Spring MVC And Java Web Learning Diary
- Java进阶学习第六天——DOM4J入门
- 【转】Eclipse 中设置JVM 内存 -- 不错
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
- Java获取目录下的所有文件
- Java 设计模式-组合模式