Java ArrayList的不同排序方法

发表于:2016-1-21 10:18

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:温布利往事    来源:51Testing软件测试网采编

  由于其功能性和灵活性,ArrayList是 Java 集合框架中使用最为普遍的集合类之一。ArrayList 是一种 List 实现,它的内部用一个动态数组来存储元素,因此 ArrayList 能够在添加和移除元素的时候进行动态的扩展和缩减。你可能已经使用过 ArrayList,因此我将略过基础部分。如果你对 ArrayList 还不熟悉,你可以参考它的 API 文档,可以很容易理解在 ArrayList 上执行基本的操作。
  In this post, I will discuss one of the most important operation on ArrayList that you will most likely require implementing during enterprise application development. It’s sorting the elements of an ArrayList.
  在这篇文章中,我将讨论 ArrayList 中一种极其重要的操作,你很有可能需要在企业应用开发中实现它。它就是 ArrayList 元素的排序。
  排序字符串对象的 ArrayList
  考虑一个 ArrayList 存储着以字符串形式存在的国名(country name),为了对这个 ArrayList 进行排序,你需要调用 Collections.sort()方法,传递由国名构成的 ArrayList 对象。这种方法将按照自然顺序(按字母升序)对元素(国名)进行排序。让我们为此来写一段代码。
  SortArrayListAscendingDescending.java
package guru.springframework.blog.sortarraylist.ascendingdescending;
import java.util.ArrayList;
import java.util.Collections;
public class SortArrayListAscendingDescending {
private ArrayList arrayList;
public SortArrayListAscendingDescending(ArrayList arrayList) {
this.arrayList = arrayList;
}
public ArrayList getArrayList() {
return this.arrayList;
}
public ArrayList sortAscending() {
Collections.sort(this.arrayList);
return this.arrayList;
}
public ArrayList sortDescending() {
Collections.sort(this.arrayList, Collections.reverseOrder());
return this.arrayList;
}
}
  在上面的类中,我们在构造器中初始化了一个 ArrayList 对象。在 sortAscending()方法中,我们调用了 Collections.sort()方法,并传递这个初始化的 ArrayList对象为参数,返回排序后的 ArrayList。在 sortDescending()方法中,我们调用重载的 Collections.sort()方法让其按照降序对元素排序,这个版本的 Collections.sort()接收ArrayList对象作为第一个参数,一个由 Collections.reverseOrder()方法返回的 Comparator 对象作为第二个参数。我们将会在稍后讲解 Comparator。为了测试排序功能,我们将写一段测试代码。
  SortArrayListAscendingDescendingTest.java
package guru.springframework.blog.sortarraylist.ascendingdescending;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class SortArrayListAscendingDescendingTest {
<a href="http://www.jobbole.com/members/madao">@Test</a>
public void testSortAscendingDescending() throws Exception {
ArrayList countryList = new ArrayList&lt;&gt;();
countryList.add("France");
countryList.add("USA");
countryList.add("India");
countryList.add("Spain");
countryList.add("England");
SortArrayListAscendingDescending sortArrayList = new SortArrayListAscendingDescending(countryList);
ArrayList unsortedArrayList = sortArrayList.getArrayList();
System.out.println("Unsorted ArrayList: " + unsortedArrayList);
ArrayList sortedArrayListAscending = sortArrayList.sortAscending();
System.out.println("Sorted ArrayList in Ascending Order : " + sortedArrayListAscending);
ArrayList sortedArrayListDescending = sortArrayList.sortDescending();
System.out.println("Sorted ArrayList in Descending Order: " + sortedArrayListDescending);
}
}
31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号