服务器之家

服务器之家 > 正文

TreeSet判断重复元素解析及代码示例

时间:2021-03-16 12:24     来源/作者:Orisun

TreeSet的底层是TreeMap的keySet(),而TreeMap是基于红黑树实现的,红黑树是一种平衡二叉查找树,它能保证任何一个节点的左右子树的高度差不会超过较矮的那棵的一倍。

TreeMap是按key排序的,所以TreeSet中的元素也是排好序的。显然元素在插入TreeSet时compareTo()方法要被调用,所以TreeSet中的元素要实现Comparable接口。TreeSet作为一种Set,它不允许出现重复元素。TreeSet是用compareTo()来判断重复元素的,而非equals(),看下面代码。

java" id="highlighter_698348">
?
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
import java.util.TreeSet;
import org.junit.Test;
public class TestTreeSet {
    class Combine implements Comparable<Combine> {
        private int p1;
        private int p2;
        public Combine(int p1, int p2) {
            this.p1 = p1;
            this.p2 = p2;
        }
        @Override
            public int hashCode() {
            return p1 * 31 + p2;
        }
        @Override
            public Boolean equals(Object obj) {
            System.out.print("whether equal " + this + " and " + obj);
            Boolean rect = false;
            if (obj instanceof Combine) {
                System.out.println("whether equal " + this + " and " + obj);
                Combine other = (Combine) obj;
                rect = (this.p1 == other.getP1() && this.p2 == other.getP2());
            }
            System.out.println(": " + rect);
            return rect;
        }
        @Override
            public int compareTo(Combine o) {
            System.out.print("compare " + this + " and " + o);
            // 排序时只考虑p1
            if (this.p1 < o.p1) {
                System.out.println(", return -1");
                return -1;
            } else if (this.p1 > o.p1) {
                System.out.println(", return 1");
                return 1;
            } else {
                System.out.println(", return 0");
                return 0;
            }
        }
        @Override
            public String toString() {
            return "(" + p1 + "," + p2 + ")";
        }
        public int getP1() {
            return p1;
        }
        public void setP1(int p1) {
            this.p1 = p1;
        }
        public int getP2() {
            return p2;
        }
        public void setP2(int p2) {
            this.p2 = p2;
        }
    }
    @Test
      public void test() {
        Combine c1 = new Combine(1, 2);
        Combine c2 = new Combine(1, 2);
        Combine c3 = new Combine(1, 3);
        Combine c4 = new Combine(5, 2);
        TreeSet<Combine> set = new TreeSet<Combine>();
        set.add(c1);
        set.add(c2);
        set.add(c3);
        set.add(c4);
        while (!set.isEmpty()) {
            //按顺序输出TreeSet中的元素
            Combine combine = set.pollFirst();
            System.out.println(combine.getP1() + "\t" + combine.getP2());
        }
    }
}

输出:

compare (1,2) and (1,2), return 0
compare (1,2) and (1,2), return 0
compare (1,3) and (1,2), return 0
compare (5,2) and (1,2), return 1
1 2
5 2

我们看到不论compareTo()返回的是不是相等,equals()方法都没有被调用。

总结

以上就是本文关于TreeSet判断重复元素解析及代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://www.cnblogs.com/zhangchaoyang/articles/5534284.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部