本文實(shí)例講述了Java實(shí)現(xiàn)的數(shù)組去重與排序操作。分享給大家供大家參考,具體如下:
這里演示Java實(shí)現(xiàn)數(shù)組去重、排序操作
文中的示例源碼編寫基于Jdk1.6+、junit4.8.2
java.util.Arrays.sort()
支持對(duì)int[]
,long[]
,short[]
,char[]
,byte[]
,float[]
,double[]
,Object[]
進(jìn)行排序
參考示例代碼片段如下
// 聲明int 數(shù)組,并初始化int[] intArry = {5,4,7,8,2,0,1,9,3,6,10};// 對(duì)int數(shù)組進(jìn)行排序Arrays.sort(intArry);
Junit 測(cè)試類源碼:
package com.gjnote.test.array;import java.util.Arrays;import org.junit.Test;public class TestArraysSort {// 聲明int 數(shù)組,并初始化int[] intArry = {5,4,7,8,2,0,1,9,3,6,10};@Testpublic void test() {// 對(duì)int數(shù)組進(jìn)行排序Arrays.sort(intArry);for (int i = 0; i < intArry.length; i++) {System.out.println(intArry[i]);}System.out.println(Arrays.toString(intArry));}}
控制臺(tái)輸出
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()
通過實(shí)現(xiàn)內(nèi)部compare
方法實(shí)現(xiàn)對(duì)象的比較
示例代碼片段如下
/*** 使用 Collections.sort(list, Comparator(){});* 對(duì)List數(shù)組排序 推薦使用方法*/public void collectionsSortElement1(List list) {Collections.sort(list, new Comparator() {@Overridepublic int compare(String o1, String o2) {// 根據(jù)實(shí)際排序需要調(diào)整compareTo對(duì)象順序return (o2).compareTo(o1);}});}
Java實(shí)現(xiàn)對(duì)List去重
方式一,使用for循環(huán)遍歷去除List中的重復(fù)元素
代碼片段如下
List tempList = new ArrayList();// 去除原始List中的重復(fù)元素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去除重復(fù)元素
TreeSet treeSet = new TreeSet(originalList);ListtempList = new ArrayList();tempList.addAll(treeSet);// treeSet 默認(rèn)的排序?yàn)樯颍鶕?jù)實(shí)際情況添加是否需要反排序Collections.reverse(tempList);
Java實(shí)現(xiàn)對(duì)List去重后排序
Junit 測(cè)試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 數(shù)組去重 元素排序** @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) {// 根據(jù)實(shí)際排序需要調(diào)整compareTo對(duì)象順序return o2.compareTo(o1);}});//outputList(tempList);System.out.println("Collections.sort:"+ (System.currentTimeMillis() - start) + "ms");}/*** 測(cè)試 使用for循環(huán)遍歷去除重復(fù)元素* Collections.sort排序*/@Testpublic void testForLoopRemoveRepeatElement() {System.out.println("testForLoopRemoveRepeatElement");long start = System.currentTimeMillis();List tempList = new ArrayList();// 去除重復(fù)元素for (String string : originalList) {if (!tempList.contains(string)) {tempList.add(string);}}// 排序collectionsSortElement(tempList);//outputList(tempList);System.out.println("使用for循環(huán)遍歷List,去除重復(fù)元素: "+ (System.currentTimeMillis() - start) + "ms");}/*** 測(cè)試 使用Set去重;* 使用Collections.sort(list, Comparator(){});排序**/@Testpublic void testSetRemoveRepeatElement() {System.out.println("testSetRemoveRepeatElement");long start = System.currentTimeMillis();// 先排序 (理論值:先排序后去重會(huì)比后排序效率更高)collectionsSortElement(originalList);// Set 利用Set元素唯一性,去重Set set = new HashSet(originalList);List tempList = new ArrayList(set);// 后排序 可以注釋先排序,開啟后排序試試運(yùn)行時(shí)間//collectionsSortElement(tempList);//outputList(tempList);System.out.println("Collections.sort排序,使用Set去重:"+ (System.currentTimeMillis() - start) + "ms");}/*** 測(cè)試 使用 TreeSet去除重復(fù)元素* 默認(rèn)排序或Collections.reverse翻轉(zhuǎn)排序*/@Testpublic void testTreeSetRemoveRepeatElement() {System.out.println("testTreeSetRemoveRepeatElement");long start = System.currentTimeMillis();TreeSettreeSet = new TreeSet(originalList);ListtempList = new ArrayList();tempList.addAll(treeSet);// treeSet 默認(rèn)的排序?yàn)樯颍鶕?jù)實(shí)際情況添加是否需要反排序Collections.reverse(tempList);//outputList(tempList);System.out.println("使用 TreeSet排序,去除重復(fù)元素:"+ (System.currentTimeMillis() - start) + "ms");}@Testpublic void testMethods() {//outputList(originalList);// List 去重 推薦方法testSetRemoveRepeatElement();// 14mstestTreeSetRemoveRepeatElement();// 20ms//testForLoopRemoveRepeatElement();// 2525ms}}
運(yùn)行testSetRemoveRepeatElement()控制臺(tái)輸出結(jié)果
testSetRemoveRepeatElement
Collections.sort:8ms
Collections.sort排序,使用Set去重:14ms
運(yùn)行testTreeSetRemoveRepeatElement()控制臺(tái)輸出結(jié)果
testTreeSetRemoveRepeatElement
使用 TreeSet排序,去除重復(fù)元素:20ms
運(yùn)行testForLoopRemoveRepeatElement()控制臺(tái)輸出結(jié)果
testForLoopRemoveRepeatElement
Collections.sort:7ms
使用for循環(huán)遍歷List,去除重復(fù)元素: 2525ms
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選