List集合多个复杂字段判断去重的案例

2025-05-29 0 46

list去重复,我们首先想到的可能是 利用listset集合,因为set集合不允许重复。所以达到这个目的。如果集合里面是简单对象,例如integerstring等等,这种可以使用这样的方式去重复。但是如果是复杂对象,即我们自己封装的对象。用list转set 却达不到去重复的目的。 所以,回归根本。 判断object对象是否一样,我们用的是其equals方法。 所以我们只需要重写equals方法,就可以达到判断对象是否重复的目的。

话不多说,上代码:

?

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
package com.test;

import java.math.bigdecimal;

import java.util.arraylist;

import java.util.arrays;

import java.util.list;

import org.apache.commons.collections.collectionutils;

public class testcollection {

//去重复之前集合

private static list<user> list = arrays.aslist(

new user("张三", bigdecimal.valueof(35.6), 18),

new user("李四", bigdecimal.valueof(85), 30),

new user("赵六", bigdecimal.valueof(66.55), 25),

new user("赵六", bigdecimal.valueof(66.55), 25),

new user("张三", bigdecimal.valueof(35.6), 18));

public static void main(string[] args) {

//排除重复

getnorepeatlist(list);

}

/**

* 去除list内复杂字段重复对象

* @param oldlist

* @return

*/

private static list<user> getnorepeatlist(list<user> oldlist){

list<user> list = new arraylist<>();

if(collectionutils.isnotempty(oldlist)){

for (user user : oldlist) {

//list去重复,内部重写equals

if(!list.contains(user)){

list.add(user);

}

}

}

//输出新集合

system.out.println("去除重复后新集合:");

if(collectionutils.isnotempty(list)){

for (user user : list) {

system.out.println(user.tostring());

}

}

return list;

}

static class user{

private string username; //姓名

private bigdecimal score;//分数

private integer age;

public string getusername() {

return username;

}

public void setusername(string username) {

this.username = username;

}

public bigdecimal getscore() {

return score;

}

public void setscore(bigdecimal score) {

this.score = score;

}

public integer getage() {

return age;

}

public void setage(integer age) {

this.age = age;

}

public user(string username, bigdecimal score, integer age) {

super();

this.username = username;

this.score = score;

this.age = age;

}

public user() {

// todo auto-generated constructor stub

}

@override

public string tostring() {

// todo auto-generated method stub

return "姓名:"+ this.username + ",年龄:" + this.age + ",分数:" + this.score;

}

/**

* 重写equals,用于比较对象属性是否包含

*/

public boolean equals(object obj) {

if (obj == null) {

return false;

}

if (this == obj) {

return true;

}

user user = (user) obj;

//多重逻辑处理,去除年龄、姓名相同的记录

if (this.getage() .compareto(user.getage())==0

&& this.getusername().equals(user.getusername())

&& this.getscore().compareto(user.getscore())==0) {

return true;

}

return false;

}

}

}

执行结果:

去除重复后新集合:
姓名:张三,年龄:18,分数:35.6
姓名:李四,年龄:30,分数:85
姓名:赵六,年龄:25,分数:66.55

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/moneyshi/article/details/72842854

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 List集合多个复杂字段判断去重的案例 https://www.kuaiidc.com/110606.html

相关文章

发表评论
暂无评论