Set集合之HashSet(1)


/**

  • Constructs a new, empty set; the backing HashMap instance has

  • default initial capacity (16) and load factor (0.75).

*/

public HashSet() {

map = new HashMap<>();

}

HashSet无参构造器,内部完成map初始化,即将map初始化为new HashMap();

最终map底层哈希表长度为16,负载因子为0.75

public HashSet(Collection<? extends E> c)


/**

  • Constructs a new set containing the elements in the specified

  • collection. The HashMap is created with default load factor

  • (0.75) and an initial capacity sufficient to contain the elements in

  • the specified collection.

  • @param c the collection whose elements are to be placed into this set

  • @throws NullPointerException if the specified collection is null

*/

public HashSet(Collection<? extends E> c) {

map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));

addAll©;

}

HashSet指定初始集合元素的构造器,内部完成map初始化

其中map初始化的容量为 c.size()/0.75 和 16的最大值

之后使用addAll将集合c中元素添加进当前HashSet集合中

public HashSet(int initialCapacity)


/**

  • Constructs a new, empty set; the backing HashMap instance has

  • the specified initial capacity and default load factor (0.75).

  • @param initialCapacity the initial capacity of the hash table

  • @throws IllegalArgumentException if the initial capacity is less

  •         than zero
    

*/

public HashSet(int initialCapacity) {

map = new HashMap<>(initialCapacity);

}

指定初始化容量的HashSet构造器

内部完成map初始化,初始化容量为指定的初始化容量,但是map底层哈希表实际容量为tableSizeFor(initialCapacity)

public HashSet(int initialCapacity, float loadFactor)


/**

  • Constructs a new, empty set; the backing HashMap instance has

  • the specified initial capacity and the specified load factor.

  • @param initialCapacity the initial capacity of the hash map

  • @param loadFactor the load factor of the hash map

  • @throws IllegalArgumentException if the initial capacity is less

  •         than zero, or if the load factor is nonpositive
    

*/

public HashSet(int initialCapacity, float loadFactor) {

map = new HashMap<>(initialCapacity, loadFactor);

}

指定初始化容量和负载因子的HashSet构造器

内部完成map的初始化,map底层哈希表的初始化容量为tableSizeFor(initialCapacity),负载因子为loadFactor

HashSet(int initialCapacity, float loadFactor, boolean dummy)


/**

  • Constructs a new, empty linked hash set. (This package private

  • constructor is only used by LinkedHashSet.) The backing

  • HashMap instance is a LinkedHashMap with the specified initial

  • capacity and the specified load factor.

  • @param initialCapacity the initial capacity of the hash map

  • @param loadFactor the load factor of the hash map

  • @param dummy ignored (distinguishes this

  •         constructor from other int, float constructor.)
    
  • @throws IllegalArgumentException if the initial capacity is less

  •         than zero, or if the load factor is nonpositive
    

*/

HashSet(int initialCapacity, float loadFactor, boolean dummy) {

map = new LinkedHashMap<>(initialCapacity, loadFactor);

}

该构造器为默认访问权限,即同包访问,即我们日常使用时,无法使用该构造器。

该构造器主要是给HashSet子类LinkedHashSet使用。

成员方法

====

public boolean add(E e)


/**

  • Adds the specified element to this set if it is not already present.

  • More formally, adds the specified element e to this set if

  • this set contains no element e2 such that

  • (enull ? e2null : e.equals(e2)).

  • If this set already contains the element, the call leaves the set

  • unchanged and returns false.

  • @param e element to be added to this set

  • @return true if this set did not already contain the specified

  • element

*/

public boolean add(E e) {

return map.put(e, PRESENT)==null;

}

可以看出HashSet的add方法内部调用HashMap的put方法

HashMap的put方法具有新增和修改功能。其中新增是对于key-valule(返回null),修改是对于value(返回老的value)

而对于HashSet而言,只关注map的key列,value数据永远时PRESENT。所以map的put修改功能对key(HashSet的集合元素)无效果,或者说put修改功能在HashSet体现为永远返回PRESENT。

(注意这里就是为什么将HashSet底层HashMap的value设置为PRESENT,而不是null的原因。如果图方便,将HashSet底层的map的value设置为null,则put重复key时,会导致put方法返回的被覆盖的value值总是null,那么就无法判断HashSet的add方法是否添加key成功了)

所以HashSet新增重复元素时,底层map的key列无变化。此时add方法返回false。

新增不重复元素时,底层map的key会新增元素,此时add方法返回true。

public boolean remove(Object o)


/**

  • Removes the specified element from this set if it is present.

  • More formally, removes an element e such that

  • (onull ? enull : o.equals(e)),

  • if this set contains such an element. Returns true if

  • this set contained the element (or equivalently, if this set

  • changed as a result of the call). (This set will not contain the

  • element once the call returns.)

  • @param o object to be removed from this set, if present

  • @return true if the set contained the specified element

*/

public boolean remove(Object o) {

return map.remove(o)==PRESENT;

}

HashSet的remove方法依赖于底层HashMap的remove方法

我们知道HashMap的remove方法,删除存在的key的键值对时,会将对应的value作为方法返回值,删除不存在的key的键值对时,会直接返回null。

而HashSet底层的HashMap的value永远是PRESENT。所以如果删除的元素(key)存在,则会返回PRESENT,否则返回null。

这就可以帮助HashSet的remove方法判断是否删除成功了。

public void clear()


/**

  • Removes all of the elements from this set.

  • The set will be empty after this call returns.

*/

public void clear() {

map.clear();

}

HashSet的clear方法依赖于底层map的clear方法

public boolean contains(Object o)


/**

  • Returns true if this set contains the specified element.

  • More formally, returns true if and only if this set

  • contains an element e such that

  • (onull ? enull : o.equals(e)).

  • @param o element whose presence in this set is to be tested

  • @return true if this set contains the specified element

*/

public boolean contains(Object o) {

return map.containsKey(o);

}

HashSet的contains方法依赖于底层map的containsKey方法

public int size()


/**

  • Returns the number of elements in this set (its cardinality).

  • @return the number of elements in this set (its cardinality)

*/

public int size() {

return map.size();

}

HashSet的size方法依赖于底层map的size方法

public boolean isEmpty()


/**

  • Returns true if this set contains no elements.

  • @return true if this set contains no elements

*/

public boolean isEmpty() {

return map.isEmpty();

}

HashSet的isEmpty方法依赖于底层map的isEmpty方法

public Object clone()

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
无助。**

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-RXAcCDvX-1715591163904)]

[外链图片转存中…(img-ONLTbFjK-1715591163905)]

[外链图片转存中…(img-2OLdr9NW-1715591163905)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值