当前位置: 首页 > article >正文

java基础之HashSet详解

HashSet详解

HashSet是基于HashMap实现的一个单列存储的集合类,将所有的数据存在HashMap的key值中,而value全部使用一个Object对象存储

继承关系

public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneablejava.io.Serializable
HashSet
HashSet

继承了AbstractSet类,实现了Set接口、Cloneable接口和Serializable接口,所以HashSet是支持克隆和序列化的

源码分析

关键变量
// 使用HashMap存储数据  map的key为HashSet的元素值
private transient HashMap<E,Object> map;

// Dummy value to associate with an Object in the backing Map
// map中所有的值都是该Object对象
private static final Object PRESENT = new Object();
构造器
// 无参构造器,直接实例化一个HashMap
public HashSet() {
    map = new HashMap<>();
}

/**
 * 使用的是HahMap中传入初始容量的构造器
 */

public HashSet(Collection<? extends E> c) {
    map = new HashMap<>(Math.max((int) (c.size()/.75f) + 116));
    addAll(c);
}

/**
 * 使用的是HahMap中传入初始容量和加载因子的构造器
 */

public HashSet(int initialCapacity, float loadFactor) {
    map = new HashMap<>(initialCapacity, loadFactor);
}

/**
 * 使用的是HahMap中传入初始容量的构造器
 */

public HashSet(int initialCapacity) {
    map = new HashMap<>(initialCapacity);
}

/**
 * 该构造器是提供给LinkedHashSet使用的,不对外暴露,实例化的是LinkedHashMap
 */

HashSet(int initialCapacity, float loadFactor, boolean dummy) {
    map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
方法分析

HashSet的方法实现都非常简单,直接使用封装的HashMap来操作数据,真正执行的是HashMap的方法

/**
 * Returns an iterator over the elements in this set.  The elements
 * are returned in no particular order.
 *
 * @return an Iterator over the elements in this set
 * @see ConcurrentModificationException
 */

public Iterator<E> iterator() {
    return map.keySet().iterator();
}

/**
 * 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();
}

/**
 * Returns <tt>true</tt> if this set contains no elements.
 *
 * @return <tt>true</tt> if this set contains no elements
 */

public boolean isEmpty() {
    return map.isEmpty();
}

/**
 * Returns <tt>true</tt> if this set contains the specified element.
 * More formally, returns <tt>true</tt> if and only if this set
 * contains an element <tt>e</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
 *
 * @param o element whose presence in this set is to be tested
 * @return <tt>true</tt> if this set contains the specified element
 */

public boolean contains(Object o) {
    return map.containsKey(o);
}

/**
 * Adds the specified element to this set if it is not already present.
 * More formally, adds the specified element <tt>e</tt> to this set if
 * this set contains no element <tt>e2</tt> such that
 * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
 * If this set already contains the element, the call leaves the set
 * unchanged and returns <tt>false</tt>.
 *
 * @param e element to be added to this set
 * @return <tt>true</tt> if this set did not already contain the specified
 * element
 */

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

/**
 * Removes the specified element from this set if it is present.
 * More formally, removes an element <tt>e</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,
 * if this set contains such an element.  Returns <tt>true</tt> 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 <tt>true</tt> if the set contained the specified element
 */

public boolean remove(Object o) {
    return map.remove(o)==PRESENT;
}

/**
 * Removes all of the elements from this set.
 * The set will be empty after this call returns.
 */

public void clear() {
    map.clear();
}

https://zhhll.icu/2021/java基础/集合/4.HasSet详解/

本文由 mdnice 多平台发布


http://www.kler.cn/news/156313.html

相关文章:

  • uniapp连接蓝牙称(接收,发送)
  • Oracle下统计平均用餐时间、用餐总量、好评率
  • Mars3d标绘的时候通过绑定单击事件,查询点击落点的图层类型
  • “微流控芯片建模与肿瘤标志物/感染性疾病细胞分析:合成生物学的新前沿“
  • 编织魔法——我与计算机的不解之缘
  • STL中使用[]重载的类
  • dart 控制台应用
  • 小程序如何刷新当前页面?
  • SQL server 2016安装
  • IEEE 机器人最优控制开源库 Model-based Optimization for Robotics
  • 【开源】基于JAVA的校园电商物流云平台
  • 【C++】sizeof()、strlen()、length()\以及size()用法区别
  • 【每日一题】1038. 从二叉搜索树到更大和树-2023.12.4
  • python-GC机制、装饰器、生成器、迭代器、三元表达式、列表生成式、生成器表达式、函数递归、面向对象、
  • 【数据结构】八大排序 (三)
  • 如何精准操作无人机自动停机坪?
  • (c语言进阶)作业讲解
  • Docker pull 命令
  • 网件R8500 trojan
  • 技术前沿丨Teranode如何实现无限扩容
  • 36、红外遥控(外部中断)
  • 数据库事务的隔离级别
  • PyQt实战 创建一个PyQt5项目
  • 【算法】滑动窗口题单——5.多指针滑动窗口⭐
  • LabVIEW通过编程将图形类控件的X轴显示为时间戳
  • easyrecovery2024绿色版中文语言电脑数据恢复工具
  • 网络层之SDN基本概念、路由算法和路由协议
  • java的弱引用、软引用和虚引用
  • Ubuntu Server 20.04.6安装Anaconda3
  • javascript中的过滤操作