本文實例講述了java實現的數組去重與排序操作。分享給大家供大家參考,具體如下:
這里演示java實現數組去重、排序操作
文中的示例源碼編寫基于jdk1.6+、junit4.8.2
java.util.arrays.sort()
支持對int[]
,long[]
,short[]
,char[]
,byte[]
,float[]
,double[]
,object[]
進行排序
參考示例代碼片段如下
1
2
3
4
|
// 聲明int 數組,并初始化 int [] intarry = { 5 , 4 , 7 , 8 , 2 , 0 , 1 , 9 , 3 , 6 , 10 }; // 對int數組進行排序 arrays.sort(intarry); |
junit 測試類源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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 }; @test public 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
方法實現對象的比較
示例代碼片段如下
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 使用 collections.sort(list, comparator(){}); * 對list數組排序 推薦使用方法 */ public void collectionssortelement1(list list) { collections.sort(list, new comparator() { @override public int compare(string o1, string o2) { // 根據實際排序需要調整compareto對象順序 return (o2).compareto(o1); } }); } |
java實現對list去重
方式一,使用for循環遍歷去除list中的重復元素
代碼片段如下
1
2
3
4
5
6
7
|
list templist = new arraylist(); // 去除原始list中的重復元素 for (string string : originallist) { if (!templist.contains(string)) { templist.add(string); } } |
方式二,使用set去重
代碼片段如下
1
2
3
|
// set 利用set元素唯一性,去重 set set = new hashset(originallist); list templist = new arraylist(set); |
方式三,使用 treeset去除重復元素
1
2
3
4
5
|
treeset treeset = new treeset(originallist); listtemplist = new arraylist(); templist.addall(treeset); // treeset 默認的排序為升序,根據實際情況添加是否需要反排序 collections.reverse(templist); |
java實現對list去重后排序
junit 測試list去重及排序源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
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 ; @before public void setup() throws exception { originallist = new arraylist(); for ( int i = 10000 ; i > 0 ; i--) { originallist.add( "element" + i); // add repeat element if (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() { @override public 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排序 */ @test public 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(){});排序 * */ @test public 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翻轉排序 */ @test public 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" ); } @test public void testmethods() { //outputlist(originallist); // list 去重 推薦方法 testsetremoverepeatelement(); // 14ms testtreesetremoverepeatelement(); // 20ms //testforloopremoverepeatelement();// 2525ms } } |
運行testsetremoverepeatelement()控制臺輸出結果
testsetremoverepeatelement
collections.sort:8ms
collections.sort排序,使用set去重:14ms
運行testtreesetremoverepeatelement()控制臺輸出結果
testtreesetremoverepeatelement
使用 treeset排序,去除重復元素:20ms
運行testforloopremoverepeatelement()控制臺輸出結果
testforloopremoverepeatelement
collections.sort:7ms
使用for循環遍歷list,去除重復元素: 2525ms
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/qq_37822393/article/details/72983734