Files
Hui-s-notebook/logseq-java/pages/TreeSet.md
2024-02-02 00:12:49 +08:00

962 B

  • 一种树形结构的集合
    • 二叉树结构
      • 每一个节点下最多两个分支
    • TreeSet结构为红黑二叉树
      • 通过红黑二叉数可提升操作和查询效率
  • 特点 id:: 625fe239-5548-4c66-ba66-f6fb1629b682
    • 元素存储有序
    • 元素不能重复
    • 没有下标
  • API与HashSet一致
  • 排序
  • 去重原理 id:: 625fa874-b218-4044-ac16-b9de892cd9d2
    • 不依靠HashCodeequals
    • 依靠比较器 Comparable
    • Key上一定要实现Comparable
    • public class Girl implements Comparable<Girl> {
          private String name;
          private String id; 
      	@Override
        	public int compareTo(Girl o) {
          if (this.equals(o)) {
            return 0;
          }
          if (this.name.equals(o.name)) {
            return this.id.compareTo(o.id);
          } else {
            return this.name.compareTo(o.name);
          }
        }
      }
      
  • HashSet的区别