java中的数组与集合的排序---摘抄自:http://blog.csdn.net/jonathan_q_bo/archive/2005/11/29/539043.aspx

简介:

java中的数组与集合的排序

两种需要排序的对象:数组和集合

1、集合

java.util.Collections类提供了排序方法sort();

static void sort(List list) 
          Sorts the specified list into ascending order, according to the natural ordering of its elements.
static void sort(List list, Comparator c) 
          Sorts the specified list according to the order induced by the specified comparator.

       两种排序方式:
      1)被排序对象实现java.lang.Comparable接口,在其中的compareTo()方法中确定排序规则
      2)用java.util.Comparator的具体实例确定排序规则

2、数组

java.util.Arrays类提供了各种重载的排序方法sort();

static void sort(byte[] a) 
          Sorts the specified array of bytes into ascending numerical order.
static void sort(byte[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of bytes into ascending numerical order.
static void sort(char[] a) 
          Sorts the specified array of chars into ascending numerical order.
static void sort(char[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of chars into ascending numerical order.
static void sort(double[] a) 
          Sorts the specified array of doubles into ascending numerical order.
static void sort(double[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of doubles into ascending numerical order.
static void sort(float[] a) 
          Sorts the specified array of floats into ascending numerical order.
static void sort(float[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of floats into ascending numerical order.
static void sort(int[] a) 
          Sorts the specified array of ints into ascending numerical order.
static void sort(int[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of ints into ascending numerical order.
static void sort(long[] a) 
          Sorts the specified array of longs into ascending numerical order.
static void sort(long[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of longs into ascending numerical order.
static void sort(Object[] a) 
          Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
static void sort(Object[] a, Comparator c) 
          Sorts the specified array of objects according to the order induced by the specified comparator.
static void sort(Object[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
static void sort(Object[] a, int fromIndex, int toIndex, Comparator c) 
          Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
static void sort(short[] a) 
          Sorts the specified array of shorts into ascending numerical order.
static void sort(short[] a, int fromIndex, int toIndex) 
          Sorts the specified range of the specified array of shorts into ascending numerical order.

如果对int[]等类型可直接升序排序
如果对Object[]类型排序,需要确定排序顺序,仍旧两种方式,被排序类本身实现Comparable接口或者提供java.util.Comparator类。

 /*
 * Created on 2005-11-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package sort;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
//import java.util.Random;
import java.lang.Math;

/**
 * @author Jonathan
 *
 * <b>排序方法汇总</b>
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SortCollection{

 private void sortByComparableOfList(){
  ArrayList al = new ArrayList();
  for(int i = 0; i < 10; i++){
   int tmp = (int)(Math.random()*10);
   al.add(new SortedObjectOne(tmp,"List." + i));
  }
  Collections.sort(al);
  Iterator itt = al.iterator();
  while(itt.hasNext())
   System.out.println(itt.next());
 }
 
 private void sortByComparatorOfList(){
  ArrayList al = new ArrayList();
  for(int i = 0; i < 10; i++){
   int tmp = (int)(Math.random()*10);
   al.add(new SortedObjectOne(tmp,"List." + i));
  }
  Collections.sort(al, new SortOrderAsc());
  Iterator itt = al.iterator();
  while(itt.hasNext())
   System.out.println(itt.next());
 }
 
 /**整形数组的默认排序*/
 private void sortByDefaultOfArray(){
  int[] array = new int[10];
  for(int i = 0; i < array.length; i++){
   array[i] = (int)(Math.random()*10);
  }
  Arrays.sort(array);
  for(int i = 0; i < array.length; i++){
   System.out.println("<Array" + i + ">:<" + array[i] + ">");
  }
 }
 
 /**对象数组的默认排序*/
 private void sortByComparableOfArray(){
  SortedObjectOne[] array = new SortedObjectOne[10];
  for(int i = 0; i < array.length; i++){
   array[i] = new SortedObjectOne((int)(Math.random()*10), "Array" + i);
  }
  Arrays.sort(array);
  for(int i = 0; i < array.length; i++){
   System.out.println(array[i]);
  }
 }
 
 /**对象数组的定制排序*/
 private void sortByComparatorOfArray(){
  SortedObjectOne[] array = new SortedObjectOne[10];
  for(int i = 0; i < array.length; i++){
   array[i] = new SortedObjectOne((int)(Math.random()*10), "Array" + i);
  }
  Arrays.sort(array, new SortOrderAsc());
  for(int i = 0; i < array.length; i++){
   System.out.println(array[i]);
  }
 }
 
 public static void main(String args[]){
  SortCollection sc = new SortCollection();
  System.out.println("-- sort list by comparable --");
  sc.sortByComparableOfList();
  System.out.println("-- sort list by comparator --");
  sc.sortByComparatorOfList();
  System.out.println("-- sort array by default --");
  sc.sortByDefaultOfArray();
  System.out.println("-- sort array by comparable --");
  sc.sortByComparableOfArray();
  System.out.println("-- sort array by comparator --");
  sc.sortByComparatorOfArray();
 }
 
}

/** 根据Comparable接口确定排序顺序 */
class SortedObjectOne implements Comparable{
 
 public int attr1;
 public String attr2;
 
 public SortedObjectOne(int attr1,String attr2){
  this.attr1 = attr1;
  this.attr2 = attr2;
 }
 
 /* (non-Javadoc)
  * @see java.lang.Comparable#compareTo(java.lang.Object)
  */
 public int compareTo(Object arg0) {
  // TODO Auto-generated method stub
  if(arg0 instanceof SortedObjectOne){
   SortedObjectOne obj = (SortedObjectOne)arg0;
   return this.attr1 - obj.attr1;
  }
  return 0;
 }
 
 
 /* (non-Javadoc)
  * @see java.lang.Object#toString()
  */
 public String toString() {
  // TODO Auto-generated method stub
  return "<" + this.attr2 + ">:<" + this.attr1 + ">";
 }
}


/** 根据Comparator确定排序顺序 */
class SortOrderAsc implements Comparator{

 /* (non-Javadoc)
  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  */
 public int compare(Object arg0, Object arg1) {
  // TODO Auto-generated method stub
  if(arg0 instanceof SortedObjectOne && arg1 instanceof SortedObjectOne){
   SortedObjectOne tmp0 = (SortedObjectOne)arg0;
   SortedObjectOne tmp1 = (SortedObjectOne)arg1;
   return tmp1.attr1 - tmp0.attr1;
  }
  return 0;
 }

}

程序执行结果:

 -- sort list by comparable --
<List.3>:<0>
<List.9>:<0>
<List.0>:<1>
<List.1>:<1>
<List.4>:<1>
<List.6>:<3>
<List.8>:<3>
<List.5>:<7>
<List.2>:<8>
<List.7>:<9>
-- sort list by comparator --
<List.7>:<5>
<List.1>:<4>
<List.2>:<3>
<List.8>:<3>
<List.3>:<2>
<List.5>:<2>
<List.9>:<2>
<List.0>:<0>
<List.4>:<0>
<List.6>:<0>
-- sort array by default --
<Array0>:<1>
<Array1>:<2>
<Array2>:<2>
<Array3>:<6>
<Array4>:<7>
<Array5>:<8>
<Array6>:<8>
<Array7>:<9>
<Array8>:<9>
<Array9>:<9>
-- sort array by comparable --
<Array2>:<0>
<Array1>:<1>
<Array7>:<1>
<Array9>:<3>
<Array0>:<4>
<Array8>:<4>
<Array5>:<5>
<Array6>:<5>
<Array3>:<8>
<Array4>:<9>
-- sort array by comparator --
<Array3>:<9>
<Array7>:<9>
<Array1>:<7>
<Array4>:<5>
<Array5>:<3>
<Array8>:<3>
<Array0>:<2>
<Array2>:<1>
<Array6>:<1>
<Array9>:<0>

本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2007/07/04/805265.html如需转载请自行联系原作者

kenty
相关文章
|
存储 算法 安全
Java集合框架:理解类型多样性与限制
总之,在 Java 题材中正确地应对多样化与约束条件要求开发人员深入理解面向对象原则、范式编程思想以及JVM工作机理等核心知识点。通过精心设计与周密规划能够有效地利用 Java 高级特征打造出既健壮又灵活易维护系统软件产品。
367 7
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
534 100
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
495 101
|
存储 Java Go
对比Java学习Go——函数、集合和OOP
Go语言的函数支持声明与调用,具备多返回值、命名返回值等特性,结合`func`关键字与类型后置语法,使函数定义简洁直观。函数可作为一等公民传递、赋值或作为参数,支持匿名函数与闭包。Go通过组合与接口实现面向对象编程,结构体定义数据,方法定义行为,接口实现多态,体现了Go语言的简洁与高效设计。
412 4
|
算法 Java
50道java集合面试题
50道 java 集合面试题
|
存储 NoSQL Java
Java Stream API:集合操作与并行处理
Stream API 是 Java 8 提供的集合处理工具,通过声明式编程简化数据操作。它支持链式调用、延迟执行和并行处理,能够高效实现过滤、转换、聚合等操作,提升代码可读性和性能。
1104 0
|
存储 缓存 安全
Java集合框架(三):Map体系与ConcurrentHashMap
本文深入解析Java中Map接口体系及其实现类,包括HashMap、ConcurrentHashMap等的工作原理与线程安全机制。内容涵盖哈希冲突解决、扩容策略、并发优化,以及不同Map实现的适用场景,助你掌握高并发编程核心技巧。
|
存储 缓存 安全
Java集合框架(二):Set接口与哈希表原理
本文深入解析Java中Set集合的工作原理及其实现机制,涵盖HashSet、LinkedHashSet和TreeSet三大实现类。从Set接口的特性出发,对比List理解去重机制,并详解哈希表原理、hashCode与equals方法的作用。进一步剖析HashSet的底层HashMap实现、LinkedHashSet的双向链表维护顺序特性,以及TreeSet基于红黑树的排序功能。文章还包含性能对比、自定义对象去重、集合运算实战和线程安全方案,帮助读者全面掌握Set的应用与选择策略。
1618 23
|
存储 安全 Java
Java集合框架(一):List接口及其实现类剖析
本文深入解析Java中List集合的实现原理,涵盖ArrayList的动态数组机制、LinkedList的链表结构、Vector与Stack的线程安全性及其不推荐使用的原因,对比了不同实现的性能与适用场景,帮助开发者根据实际需求选择合适的List实现。
1467 0
|
安全 Java 开发者
Java集合框架:详解Deque接口的栈操作方法全集
理解和掌握这些方法对于实现像浏览器后退功能这样的栈操作来说至关重要,它们能够帮助开发者编写既高效又稳定的应用程序。此外,在多线程环境中想保证线程安全,可以考虑使用ConcurrentLinkedDeque,它是Deque的线程安全版本,尽管它并未直接实现栈操作的方法,但是Deque的接口方法可以相对应地使用。
730 12