Java HashSet工作原理及实现
2021-05-23 01:29
标签:数学 bool actually nbsp color add ash obj 分享图片 1. 概述 HashSet是基于HashMap来实现的,操作很简单,更像是对HashMap做了一次“封装”,而且只使用了HashMap的key来实现各种特性,我们先来感性的认识一下这个结构: 其大致的结构是这样的:
map是整个HashSet的核心,而PRESENT则是用来造一个假的value来用的。Map有键和值,HashSet相当于只有键,值都是相同的固定值,即PRESENT。 2. 基本操作 基本操作也非常简单,就是调用HashMap的相关方法,其中value就是之前那个dummy的Object。 Refer: http://yikun.github.io/2015/04/08/Java-HashSet%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/ Java HashSet工作原理及实现 标签:数学 bool actually nbsp color add ash obj 分享图片 原文地址:https://www.cnblogs.com/winner-0715/p/9736812.htmlThis class implements the Set interface, backed by a hash table (actually a HashMap instance).
It makes no guarantees as to the iteration order of the set; in particular,
it does not guarantee that the order will remain constant over time.
This class permits the null element.This class implements the Set interface, backed by a hash table (actually a HashMap instance).
It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
This class permits the null element.
HashSet
private transient HashMapE,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
public boolean contains(Object o) {
return map.containsKey(o);
}
public int size() {
return map.size();
}
文章标题:Java HashSet工作原理及实现
文章链接:http://soscw.com/index.php/essay/88066.html