集合Set接口

Set

set接口是继承自Collection的子接口,特点是元素不重复,存储无序。

HashSet

HashSet 通过元素的两个方法,hashCode和equals 方法来保证元素的唯一性, 如果元素的HashCode值相同,才会判断equals是否为true

如果元素的hashCode值不同,不会调用equals方法

对于HashSet而言,它是基于HashMap实现的,HashSet底层使用HashMap来保存所有元素,因此HashSet 的实现比较简单,相关HashSet的操作,基本上都是直接调用底层HashMap的相关方法来完成。

    //底层使用HashMap 来保存 HashSet中所有元素
	private transient HashMap<E,Object> map;
	//定义一个Object对象作为HashMap 的value,此对象是 static final。
    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();
	//默认无参构造器
	//实际 底层会初始化一个空的HashMap ,并使用默认初始容量16,加载因子0.75
	public HashSet() {
        map = new HashMap<>();
    }
	// 构造一个包含 Collection 中的元素的 Hashset
	//实际底层使用默认的加载因子0.75和足以包含指定collection中所有元素的初始容量来创建一个HashMap。 
     //其中的元素将存放在此set中的collection。 
	 public HashSet(Collection<? extends E> c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
    }
	//以指定的initialCapacity和loadFactor构造一个空的HashSet。 底层对应的是响应参数得HashMap
	public HashSet(int initialCapacity, float loadFactor) {
        map = new HashMap<>(initialCapacity, loadFactor);
    }
	// 给定initialCapacity 创建一个 HashSet
	public HashSet(int initialCapacity) {
        map = new HashMap<>(initialCapacity);
    }
	//以 initialCapacity  loadFactor 构建一个新。实际只是对LinkedHshSet 支持的
	// 底层是 以指定参数构建一个 LinkedHashMap 
	// dummy 标记
	HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }
	//其它的一些操作 也是根据hashmap

LinkedHashSet

对于LinkedHashSet而言,它继承与HashSet、又基于LinkedHashMap来实现的。

LinkedHashSet底层使用LinkedHashMap来保存所有元素,它继承与HashSet,其所有的方法操作上又与HashSet相同,因此LinkedHashSet 的实现上非常简单,只提供了四个构造方法,并通过传递一个标识参数,调用父类的构造器,底层构造一个LinkedHashMap来实现,在相关操作上与父类HashSet的操作相同,直接调用父类HashSet的方法即可。

public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable {

    private static final long serialVersionUID = -2851667679971038690L;

    /**
     * Constructs a new, empty linked hash set with the specified initial
     * capacity and load factor.
     *
     * @param      initialCapacity the initial capacity of the linked hash set
     * @param      loadFactor      the load factor of the linked hash set
     * @throws     IllegalArgumentException  if the initial capacity is less
     *               than zero, or if the load factor is nonpositive
     *构造一个带有指定初始容量和加载因子的新空链接哈希set。
     *底层会调用父类的构造方法,构造一个有指定初始容量和加载因子的LinkedHashMap实例
     */
    public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    }

    /**
     * Constructs a new, empty linked hash set with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param   initialCapacity   the initial capacity of the LinkedHashSet
     * @throws  IllegalArgumentException if the initial capacity is less
     *              than zero
     * 构造一个带指定初始容量和默认加载因子0.75的新空链接哈希set
     * 底层会调用父类的构造方法,构造一个带指定初始容量和默认加载因子0.75的LinkedHashMap实例。
     */
    public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }

    /**
     * Constructs a new, empty linked hash set with the default initial
     * capacity (16) and load factor (0.75).
     * 初始16  默认.75的
     * 底层会调用父类的构造方法,构造一个带默认初始容量16和加载因子0.75的LinkedHashMap实例。
     */
    public LinkedHashSet() {
        super(16, .75f, true);
    }

    /**
     * Constructs a new linked hash set with the same elements as the
     * specified collection.  The linked hash set is created with an initial
     * capacity sufficient to hold the elements in the specified collection
     * and the default load factor (0.75).
     *
     * @param c  the collection whose elements are to be placed into
     *           this set
     * @throws NullPointerException if the specified collection is null
     * 构造一个与指定collection中的元素相同的新链接哈希set。
     * 底层会调用父类的构造方法,构造一个足以包含指定collection
     * 中所有元素的初始容量和加载因子为0.75的LinkedHashMap实例。
     */
    public LinkedHashSet(Collection<? extends E> c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }
}

TreeSet

	    TreeSet treeSet = new TreeSet();
        treeSet.add(3);
        treeSet.add(6);
        treeSet.add(9);
        System.out.println(treeSet);
        //返回此集合中 最小元素大于或等于  给定元素,没有返回null
		System.out.println(treeSet.ceiling(10));
        System.out.println(treeSet.floor(1));   //小于等于的 ~
		System.out.println(treeSet.last());//返回集合中当前的最后元素
        System.out.println(treeSet.first());//返回集合中当前的第一个元素


 // new了一个 TreeMap
	public TreeSet() {// 无参构造
        this(new TreeMap<E,Object>());
    }
// TreeMap :  A Red-Black tree  是一个红黑树
// 使用元素的自然顺序对元素进行排序,或者根据创建 set 时提供的 Comparator 进行排序,具体取决于使用的构造方法。
// TreeSet保证元素的排序和唯一性的
	public TreeSet(Comparator<? super E> comparator) {//包含比较器的构造
        this(new TreeMap<>(comparator));
    }

树的简单介绍: 

 AVL树:

 AVL树 左旋右旋

红黑树:

 红黑树介绍 : 是一种自平衡二叉查找树,与AVL树类似,都在插入和删除操作时能通过旋转操作保持二叉查找树的平衡 变色操作 红黑树 牺牲了部分平衡性以换取 插入/删除 时少了的旋转操作,整体来说性能要优于AVL树

特点 : 节点是红色或黑色 根节点是黑色

每个叶子节点是黑色 每个红色节点的两个子节点 都为黑色,(从每个叶子到根节点的所有路径上不能右两个连续的红色节点)

从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。

最长路径不超过最短路径的2倍

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值