Java TreeSet 添加失败的解决

2025-05-29 0 59

JavaTreeSet 是Set的一个子类。

Set、List、Map区别

Set是一个无序、不允许重复的集合。

List(ArrayList、Vector等)是有序、可重复的。

Map(HashMap等)是键值对。

?

1

2

3

4

5

6

7

8

9
public static void demo() {

TreeSet<Person> ts = new TreeSet<>();

ts.add(new Person("张三", 23));

ts.add(new Person("李四", 13));

ts.add(new Person("周七", 13));

ts.add(new Person("王五", 43));

ts.add(new Person("赵六", 33));

System.out.println(ts);

}

此处会报异常,异常类型为java.lang.ClassCastException

这是因为Person类没有实现Comparable 接口,并实现compareTo函数。

compareTo函数就是TreeSet用来判断是否唯一的函数。

?

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
public class TreeEntity implements Comparable<Object> {

@JsonProperty(value = "Name")

private String name = "";

/**

* 名称(字段名)

*

*/

public final String getName() {

return name;

}

public final void setName(String value) {

name = value;

}

@JsonProperty(value = "Header")

private String header = "";

/**

* 显示的名称(字段别名)

*/

public final String getHeader() {

if (header.isEmpty()) {

header = name;

}

return header;

}

public final void setHeader(String value) {

header = value;

}

@Setter(AccessLevel.PROTECTED)

@JsonProperty(value = "Childrens")

private TreeSet<TreeEntity> childrens;

/**

* 子节点集合

*

*/

public final TreeSet<TreeEntity> getChildrens() {

return childrens;

}

public final void setChildrens(TreeSet<TreeEntity> value) {

childrens = value;

}

@Override

public int compareTo(Object o) {

TreeEntity te = (TreeEntity) o;

if (te == null)

return 1;

return this.getName().compareTo(te.getName());

}

/**

* 构造函数

*/

public TreeEntity() {

childrens = new TreeSet<TreeEntity>();

}

}

}

}

入上图中的TreeEntity类重写了compareTo函数,则是通过name属性来判断是否唯一。

TreeSet.add()函数中,会触发此compareTo函数,如果判断不唯一,不会添加进去,但是代码不会报异常。

compareTo函数返回值说明:

为什么返回0,只会存一个元素,返回-1会倒序存储,返回1会怎么存就怎么取呢?原因在于TreeSet底层其实是一个二叉树机构,且每插入一个新元素(第一个除外)都会调用compareTo()方法去和上一个插入的元素作比较,并按二叉树的结构进行排列。

1. 如果将compareTo()返回值写死为0,元素值每次比较,都认为是相同的元素,这时就不再向TreeSet中插入除第一个外的新元素。所以TreeSet中就只存在插入的第一个元素。

2. 如果将compareTo()返回值写死为1,元素值每次比较,都认为新插入的元素比上一个元素大,于是二叉树存储时,会存在根的右侧,读取时就是正序排列的。

3. 如果将compareTo()返回值写死为-1,元素值每次比较,都认为新插入的元素比上一个元素小,于是二叉树存储时,会存在根的左侧,读取时就是倒序序排列的。

补充知识:compareTo方法重写错误导致TreeSet中无法添加对象

问题描述:

定义了一个实现Comparable接口的类R,包含一个int变量count。在测试类中添加了一个count为-3的对象后,便无法添加count为1的对象。但是可以添加count比-3小的和count为正数的R对象。

错误原因:

compareTo方法重写错误。

代码:

?

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
package test20160302;

import java.util.TreeSet;

class R implements Comparable<Object>{

int count;

public R(int count){

this.count = count;

System.out.println("count:"+count);

}

public String toString(){

return "R[count:"+this.count+"]";

}

public boolean equals(Object obj){

if(this == obj)return true;

if(obj!=null && obj.getClass()==R.class){

return this.count == ((R)obj).count;

}else

return false;

}

public int compareTo(Object obj){

R r = (R)obj;

System.out.println("用来比较的数:"+this.count);

System.out.println("被比较的数:"+r.count);

return this.count<r.count?-1:this.count>1?1:0;

}

}

public class TreeSetTest3 {

public static void main(String[] args) {

TreeSet ts = new TreeSet();

ts.add(new R(-3));

ts.add(new R(-1));

System.out.println(ts);

}

}

输出:

count:-3

用来比较的数:-3

被比较的数:-3

count:-1

用来比较的数:-1

被比较的数:-3

[R[count:-3]]

测试:

– 只添加-3,9,1无法添加

– 只添加9,除0外均可以添加。

– 添加-2,9后,1无法添加

– 添加-1,9后,1无法添加

– 添加-1,2后,1无法添加

– 添加-3后,-1无法添加

– 添加-1后,-3无法添加

以上这篇Java TreeSet 添加失败的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/loveyou388i/article/details/80134346

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java TreeSet 添加失败的解决 https://www.kuaiidc.com/116530.html

相关文章

发表评论
暂无评论