Java实现的数组去重与排序操作详解

2025-05-29 0 87

本文实例讲述了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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现的数组去重与排序操作详解 https://www.kuaiidc.com/111279.html

相关文章

发表评论
暂无评论