HashSet

本文详细介绍了HashSet类的功能和使用方法,包括构造函数、添加、删除、查询等操作,并解释了HashSet如何利用HashMap实现高效的元素存储和检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HashSet是外部STL中的类,用于向收集器中存储或快速检索数据。收集器中的这些数据唯一,且数值充当关键字。

添加元素:

hashset.add(E e):返回boolean型,如果此 set 中尚未包含指定元素,则添加指定元素;如果此 set 已包含该元素,则该调用不更改 set 并返回 false。

删除元素:
hashset.clear():从此 set 中移除所有元素。
hashset.remove(Object o):如果指定元素存在于此 set 中,则将其移除。
hashset.isEmpty():如果此 set 不包含任何元素,则返回 true。
hashset.contains(Object o):如果此 set 包含指定元素,则返回 true。

hashset.size():返回此 set 中的元素的数量(set 的容量)。


Constructor and Description
HashSet()
Constructs a new, empty set; the backing  HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
HashSet(int initialCapacity)
Constructs a new, empty set; the backing  HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing  HashMap instance has the specified initial capacity and the specified load factor.


 Method and Description
booleanadd(E e)
Adds the specified element to this set if it is not already present.
voidclear()
Removes all of the elements from this set.
Objectclone()
Returns a shallow copy of this  HashSet instance: the elements themselves are not cloned.
booleancontains(Object o)
Returns  true if this set contains the specified element.
booleanisEmpty()
Returns  true if this set contains no elements.
Iterator<E>iterator()
Returns an iterator over the elements in this set.
booleanremove(Object o)
Removes the specified element from this set if it is present.
intsize()
Returns the number of elements in this set (its cardinality).
Spliterator<E>spliterator()
Creates a  late-binding and  fail-fast  Spliterator over the elements in this set.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值