本文實例講述了Java實現的數組去重與排序操作。分享給大家供大家參考,具體如下:
這裡演示Java實現數組去重、排序操作
文中的示例源碼編寫基於Jdk1.6+、junit4.8.2
java.util.Arrays.sort()
支持對int[] , long[] , short[] , char[] , byte[] , float[] , double[] , Object[]進行排序
參考示例代碼片段如下
// 聲明int 數組,並初始化int[] intArry = {5,4,7,8,2,0,1,9,3,6,10};// 對int數組進行排序Arrays.sort(intArry);Junit 測試類源碼:
package com.gjnote.test.array;import java.util.Arrays;import org.junit.Test;public class TestArraysSort {// 聲明int 數組,並初始化int[] intArry = {5,4,7,8,2,0,1,9,3,6,10};@Testpublic void test() {// 對int數組進行排序Arrays.sort(intArry);for (int i = 0; i < intArry.length; i++) {System.out.println(intArry[i]);}System.out.println(Arrays.toString(intArry));}}控制台輸出
0
1
2
3
4
5
6
7
8
9
10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
java.util.Collections.sort()
通過實現內部compare方法實現對象的比較
示例代碼片段如下
/*** 使用Collections.sort(list, Comparator(){});* 對List數組排序推薦使用方法*/public void collectionsSortElement1(List list) {Collections.sort(list, new Comparator() {@Overridepublic int compare(String o1, String o2) {// 根據實際排序需要調整compareTo對象順序return (o2).compareTo(o1);}});}Java實現對List去重
方式一,使用for循環遍歷去除List中的重複元素
代碼片段如下
List tempList = new ArrayList();// 去除原始List中的重複元素for (String string : originalList) {if (!tempList.contains(string)) {tempList.add(string);}}方式二,使用Set去重
代碼片段如下
// Set 利用Set元素唯一性,去重Set set = new HashSet(originalList);List tempList = new ArrayList(set);
方式三,使用TreeSet去除重複元素
TreeSet treeSet = new TreeSet(originalList);ListtempList = new ArrayList();tempList.addAll(treeSet);// treeSet 默認的排序為升序,根據實際情況添加是否需要反排序Collections.reverse(tempList);
Java實現對List去重後排序
Junit 測試List去重及排序源碼
package com.gjnote.test.array;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.TreeSet;import org.junit.Before;import org.junit.Test;/*** Test Class*List 數組去重元素排序** @version 1.0* @author www.gjnote.com**/public class TestListArraySort {private ListoriginalList = null;@Beforepublic void setUp() throws Exception {originalList = new ArrayList();for (int i = 10000; i > 0; i--) {originalList.add("element" + i);// add repeat elementif(i % 2 == 0) {originalList.add("element" + i);}}}/*** 輸出List 元素* @param list*/private void outputList(List list) {for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}}/*** 使用Collections.sort(list, Comparator(){});* 排序推薦方法*/private void collectionsSortElement(List list) {long start = System.currentTimeMillis();Collections.sort(list, new Comparator() {@Overridepublic int compare(String o1, String o2) {// 根據實際排序需要調整compareTo對象順序return o2.compareTo(o1);}});//outputList(tempList);System.out.println("Collections.sort:"+ (System.currentTimeMillis() - start) + "ms");}/*** 測試使用for循環遍歷去除重複元素* Collections.sort排序*/@Testpublic void testForLoopRemoveRepeatElement() {System.out.println("testForLoopRemoveRepeatElement");long start = System.currentTimeMillis();List tempList = new ArrayList();// 去除重複元素for (String string : originalList) {if (!tempList.contains(string)) {tempList.add(string);}}// 排序collectionsSortElement(tempList);//outputList(tempList);System.out.println("使用for循環遍歷List,去除重複元素: "+ (System.currentTimeMillis() - start) + "ms");}/*** 測試使用Set去重;* 使用Collections.sort(list, Comparator(){});排序**/@Testpublic void testSetRemoveRepeatElement() {System.out.println("testSetRemoveRepeatElement");long start = System.currentTimeMillis();// 先排序(理論值:先排序後去重會比後排序效率更高)collectionsSortElement(originalList);// Set 利用Set元素唯一性,去重Set set = new HashSet(originalList);List tempList = new ArrayList(set);// 後排序可以註釋先排序,開啟後排序試試運行時間//collectionsSortElement(tempList);//outputList(tempList);System.out.println("Collections.sort排序,使用Set去重:"+ (System.currentTimeMillis() - start) + "ms");}/*** 測試使用TreeSet去除重複元素* 默認排序或Collections.reverse翻轉排序*/@Testpublic void testTreeSetRemoveRepeatElement() {System.out.println("testTreeSetRemoveRepeatElement");long start = System.currentTimeMillis();TreeSettreeSet = new TreeSet(originalList);ListtempList = new ArrayList();tempList.addAll(treeSet);// treeSet 默認的排序為升序,根據實際情況添加是否需要反排序Collections.reverse(tempList);//outputList(tempList);System.out.println("使用TreeSet排序,去除重複元素:"+ (System.currentTimeMillis() - start) + "ms");}@Testpublic void testMethods() {//outputList(originalList);// List 去重推薦方法testSetRemoveRepeatElement();// 14mstestTreeSetRemoveRepeatElement();// 20ms//testForLoopRemoveRepeatElement();// 2525ms}}運行testSetRemoveRepeatElement()控制台輸出結果
testSetRemoveRepeatElement
Collections.sort:8ms
Collections.sort排序,使用Set去重:14ms
運行testTreeSetRemoveRepeatElement()控制台輸出結果
testTreeSetRemoveRepeatElement
使用TreeSet排序,去除重複元素:20ms
運行testForLoopRemoveRepeatElement()控制台輸出結果
testForLoopRemoveRepeatElement
Collections.sort:7ms
使用for循環遍歷List,去除重複元素: 2525ms
PS:這裡再為大家提供幾款相關工具供大家參考使用:
在線去除重複項工具:
http://tools.VeVB.COm/code/quchong
在線文本去重複工具:
http://tools.VeVB.COm/aideddesign/txt_quchong
在線動畫演示插入/選擇/冒泡/歸併/希爾/快速排序算法過程工具:
http://tools.VeVB.COm/aideddesign/paixu_ys
更多關於java算法相關內容感興趣的讀者可查看本站專題:《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。